当前位置:网站首页>【Unity入门计划】2D游戏实现敌人来回移动控制脚本
【Unity入门计划】2D游戏实现敌人来回移动控制脚本
2022-08-10 07:59:00 【flashinggg】
目录
学习的教程
【unity2021入门教程】74-2D游戏开发教程系列-03-RubyAdventure2DRpg官方教程-22-移动敌人方式2_哔哩哔哩_bilibili
移动脚本
这里教程中列举出了两种控制移动的版本,我又自己写了一版可以实现简单的控制敌人移动方向、移动速度、以及沿着x轴或y轴移动的脚本,这里贴出来记录。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
Rigidbody2D robotrigidbody2D;
//移动速度
public int moveSpeed;
//移动最大距离
public float moveDistance;
//false为Y轴移动,true为水平移动
public bool X;
//移动方向,令初始移动方向为轴的正方向
int direction = 1;
//初始坐标值
float initx, inity;
//存放当前位置
Vector2 position;
//储存当前与起始点距离
float currentDis;
void Start()
{
robotrigidbody2D = GetComponent<Rigidbody2D>();
//移动起点
initx = transform.position.x;
inity = transform.position.y;
}
// Update is called once per frame
void Update()
{
}
//物理引擎相关的操作都要发放在这里
//默认0.02s执行一次
private void FixedUpdate()
{
//获取每帧当前的刚体位置
position = robotrigidbody2D.position;
if (X)//x轴移动
{
position.x += moveSpeed * direction * Time.deltaTime;
currentDis = Mathf.Abs(position.x - initx);
if (currentDis < 0.01f || currentDis > moveDistance)
{
direction *= -1;
}
}
else//y轴移动
{
position.y += moveSpeed * direction * Time.deltaTime;
currentDis = Mathf.Abs(position.y - inity);
if (currentDis < 0.01f || currentDis > moveDistance)
{
direction *= -1;
}
}
//更改位置
robotrigidbody2D.MovePosition(position);
//robotrigidbody2D.position = position;
}
}
脚本中,用了一个初始值为1的变量direction用来改变移动方向;如果判断需要改变移动方向了,则乘上1,即脚本中的
direction *= -1;
direction自始至终都是在-1和1之间来回切换值,也实现了移动方向的改变。
控制界面

效果
X√

X 未选(y轴)

边栏推荐
- Obtain - 65 [chances] : "soldiers, subtlety also - 7-36 meter reading - defeat
- Nude speech - lying flat - brushing questions - big factory (several tips for Android interviews)
- 34. Talk about why you want to split the database?What methods are there?
- 复杂AB实验
- 自动化测试框架Pytest(三)——自定义allure测试报告
- Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
- 941 · 滑动拼图
- PLSQL学习第四天
- js函数聚合的三种实现方式
- 30条实用MySQL优化法则
猜你喜欢

iwemeta元宇宙:阿里首任COO:如何打造销售铁军

明明加了唯一索引,为什么还是产生重复数据?

Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen

iwemeta元宇宙:一个娃娃卖9999元,泡泡玛特认为一点也不贵

AFNetworking概述和4.0的实践

初使jest 单元测试

神经网络样本太少怎么办,神经网络训练样本太少

协同工具满足70%-90%的工作需求,成为企业香饽饽

Synchronization lock synchronized traces the source

短视频同城流量宣传小魔推有何优势?如何给实体商家带来销量?
随机推荐
ABAP Data Types 和XSD Type 映射关系以及XSD Type属性
The probability distribution and its application
时序动作定位 | ASM-Loc:弱监督时序动作定位的动作感知片段建模(CVPR 2022)
DGIOT 30 million meters set pressure reading
【Event Preview on August 9】Prometheus Summit
placeholder 1
【MySQL】使用MySQL Workbench软件新建表
物联网时代下的网络整合推广外包精准化效果-深圳双赢世讯
SQL建表问题,帮我看看好吗朋友们~大家人。!
NPU architecture and force analysis
Complex AB experiment
VS2013-debug assembly code-generate asm file-structure memory layout-function parameter stack-calling convention
NPU架构与算力分析
90. (cesium house) cesium height monitoring events
IDLE development wordCount program (5)
Rust learning: 6.4_ enumeration of composite types
PLSQL学习第一天
快速输入当前日期与时间
一文2600字手把手教你编写性能测试用例
LaTeX出现错误代码Command \algorithmic already defined