public Vector3 velocity ;

描述

该角色的当前相对速度(请参阅注释)。

这可使您跟踪该角色的实际行走速度,例如 当它卡在一面墙的位置时,此值将为零向量。

注意:在调用 CharacterController.MoveCharacterController.SimpleMove 之前和之后, 返回的速度对于当前时间间隔而言在距离上是不同的。 该速度为相对速度,因为它不会跟踪朝向在 CharacterController 之外 发生的变换组件的移动(例如,作为另一个移动 Transform(例如正在移动的车辆)的 子对象的角色)。

using UnityEngine;

public class Example : MonoBehaviour { CharacterController controller; void Start() { controller = GetComponent<CharacterController>(); }

void Update() { Vector3 horizontalVelocity = controller.velocity; horizontalVelocity = new Vector3(controller.velocity.x, 0, controller.velocity.z);

// The speed on the x-z plane ignoring any speed float horizontalSpeed = horizontalVelocity.magnitude; // The speed from gravity or jumping float verticalSpeed = controller.velocity.y; // The overall speed float overallSpeed = controller.velocity.magnitude; } }