The current relative velocity of the Character (see notes).
function Update () { var controller : CharacterController = GetComponent(CharacterController); var horizontalVelocity : Vector3 = controller.velocity; horizontalVelocity = Vector3(controller.velocity.x, 0, controller.velocity.z); // The speed on the x-z plane ignoring any speed var horizontalSpeed : float = horizontalVelocity.magnitude; // The speed from gravity or jumping var verticalSpeed : float = controller.velocity.y; // The overall speed var overallSpeed : float = controller.velocity.magnitude; }
using UnityEngine; using System.Collections; public class Example : MonoBehaviour { void Update() { CharacterController controller = GetComponent<CharacterController>(); Vector3 horizontalVelocity = controller.velocity; horizontalVelocity = new Vector3(controller.velocity.x, 0, controller.velocity.z); float horizontalSpeed = horizontalVelocity.magnitude; float verticalSpeed = controller.velocity.y; float overallSpeed = controller.velocity.magnitude; } }
import UnityEngine import System.Collections public class Example(MonoBehaviour): def Update() as void: controller as CharacterController = GetComponent[of CharacterController]() horizontalVelocity as Vector3 = controller.velocity horizontalVelocity = Vector3(controller.velocity.x, 0, controller.velocity.z) horizontalSpeed as float = horizontalVelocity.magnitude verticalSpeed as float = controller.velocity.y overallSpeed as float = controller.velocity.magnitude