以 speed
的速度移动角色。
忽略沿 y 轴的速度。速度以单位/秒为单位。自动应用重力。返回角色是否接地。建议您每帧只调用一次 Move 或 SimpleMove。
using UnityEngine; using System.Collections;
[RequireComponent(typeof(CharacterController))] public class ExampleClass : MonoBehaviour { public float speed = 3.0F; public float rotateSpeed = 3.0F;
void Update() { CharacterController controller = GetComponent<CharacterController>();
// Rotate around y - axis transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
// Move forward / backward Vector3 forward = transform.TransformDirection(Vector3.forward); float curSpeed = speed * Input.GetAxis("Vertical"); controller.SimpleMove(forward * curSpeed); } }