用C#控制unity3D人物移动
可以使用以下代码来控制Unity3D中的人物移动:
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 5f; // 移动速度
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
rb.velocity = movement * speed;
}
}
在Unity中,创建一个空物体并将此脚本绑定到该物体上。确保还有一个Rigidbody组件附加到该物体上以便进行物理模拟。
在Update函数中,我们使用Input.GetAxis函数来获取玩家输入的水平和垂直方向上的移动值。然后,我们创建一个Vector3对象来存储移动方向,并将其乘以速度来得到最终的移动速度。最后,我们将移动速度分配给Rigidbody组件的velocity属性,使人物进行移动
原文地址: http://www.cveoy.top/t/topic/h1Bd 著作权归作者所有。请勿转载和采集!