[RequireComponent(typeof(CharacterController))] public class MoveTest : MonoBehaviour { private Rigidbody myRigidbody; private CharacterController myCharacterController; private Vector3 moveDirection = Vector3.zero; public float my_重力 = 20f; public float my_速度 = 5f; public float my_跳跃速度 = 10f; //人物的各种状态,包括可超控,不可操作,不能移动,不能攻击,死亡等 public UnitStateEnum my_状态 = UnitStateEnum.free; // Use this for initialization void Start () { //定义CharacterController组件 myCharacterController = transform.GetComponent<CharacterController>(); //设定人物不可穿透 myCharacterController.isTrigger = false; } void Update () { } void FixedUpdate() { //当人物在地面上时候,并且可控制状态 if (myCharacterController.isGrounded&&( my_状态== UnitStateEnum.free|| my_状态 == UnitStateEnum.slow)) { //移动方向等于Input的横轴和纵轴位移,Y(上下方向不变) moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); //让人物面朝着你需要移动的方向 transform.LookAt(transform.position+ moveDirection); //定义速度 moveDirection *= my_速度; //跳跃,如果获取到了跳跃键,则Y轴跳跃 if (Input.GetButton("Jump")) moveDirection.y = my_跳跃速度; } //附加重力因素,Y的方向一直在向下受重力影响 moveDirection.y -= my_重力 * Time.deltaTime; //向控制的方向移动 myCharacterController.Move(moveDirection * Time.deltaTime); } }