当前位置:网站首页>【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轴)
边栏推荐
- 颜色选择器的使用
- If the data of the oracle business table is added, deleted, or modified, will the index of the table write redo and undo?
- 协同工具满足70%-90%的工作需求,成为企业香饽饽
- The precise effect of network integration promotion outsourcing in the era of Internet of Things-Shenzhen Win-Win World News
- Uni-app开发微信小程序使用本地图片做背景图
- 神经网络样本太少怎么办,神经网络训练样本太少
- Rust学习:6.1_复合类型之切片
- Rust学习:6.5_复合类型之数组
- 时序动作定位 | ACGNet:弱监督时序动作定位的动作补充图网络(AAAI 2022)
- 941 · Sliding Puzzles
猜你喜欢
随机推荐
阿里云数据库 RDS SQL Server 版的服务器绑定域名www.cxsdkt.cn.的呢?
LaTeX出现错误代码Command \algorithmic already defined
DGIOT支持工业设备租赁以及远程管控
大佬,oracle单表增量同步时候源库服务器额外占用内存近2g,这不正常吧
SQL SERVER 数据库,表的数据发生增删改,该表的索引会在ldf日志中记录吗?
2022-08-01 网工进阶(二十三) VLAN高级技术-VLAN聚合、MUX VLAN
JS reduce
VS2013-debug assembly code-generate asm file-structure memory layout-function parameter stack-calling convention
CV-人脸识别-2018:ArcFace
ATH10 sensor reads temperature and humidity
Big guy, when Oracle single-table incremental synchronization, the source database server takes up nearly 2g of memory. This is not normal, right?
Add spark related dependencies and packaging plugins (sixth bullet)
In the SQL SERVER database, if the data of the table is added, deleted, or modified, will the index of the table be recorded in the ldf log?
AFNetworking概述和4.0的实践
关于数据库中的中文模糊检索探讨
phpstudy开机自启
卷积神经网络卷积层公式,卷积神经网络运算公式
Using the color picker
2022-08-01 Advanced Network Engineering (24) STP Advanced Knowledge
Rust learning: 6.4_ enumeration of composite types