使用Input.GetAxis(“Horizontal”) 和 “Vertical”得到垂直和水平方向的值
使用CharacterController.SimpleMove(Vector3)参数表示运动的方向和速度 单位可以认为是 m/s
代码如下:
private CharacterController cc;
public float speed = 4;
void Start()
{
cc = GetComponent<CharacterController>();
}
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if (Mathf.Abs(h)>0.1f||Mathf.Abs(v)>0.1)
{
Vector3 targetDir = new Vector3(h, 0, v);
transform.LookAt(targetDir+transform.position);
cc.SimpleMove(transform.forward * speed);
}
}
speed 是控制人物移动的速度
float h 获取的是操纵杆输入和键盘输入,值为(-1到1)的值,x轴正方向为1,负方向为-1,也就是说A键为-1,D键为1
float v获取的是操纵杆输入和键盘输入,值为(-1到1)的值,y轴正方向为1,负方向为-1,也就是说W键为1,S键为01
targetDir 是键盘输入之后获取到的方向,将目标用SimpleMove方法向获取到方向移动
transform.lookat 是让目标旋转到获取到的方向
transform.forward 是让目标向正前方移动
本文来自 恬静的小魔龙 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/q764424567/article/details/77984369?utm_source=copy