horizontal = Input.GetAxis("Horizontal");
if (horizontal < 0.1&&horizontal>-0.1)
horizontal = 0;
vertical = Input.GetAxis("Vertical");
if (vertical < 0.1&& vertical> -0.1)
vertical = 0;
//tran是物体的transform game1是摄像机的gameObject
if (horizontal != 0 || vertical != 0)
{
Vector3 targetDirection = new Vector3(horizontal, 0, vertical);
float y = game1.transform.rotation.eulerAngles.y;
targetDirection = Quaternion.Euler(0, y, 0) * targetDirection;
game1.transform.Translate(targetDirection * Time.deltaTime * movespeed, Space.World);
tran.Translate(targetDirection * Time.deltaTime * movespeed, Space.World);
}
这段博客内容主要展示了一段代码,用于处理游戏中物体和摄像机的移动。通过获取水平和垂直输入轴的值,对其进行处理,当输入值在一定范围内则置为 0。若有有效输入,计算目标方向并结合摄像机旋转,实现物体和摄像机按指定速度移动。
1万+





