CharacterController.velocity 速度

var velocity : Vector3

Description描述

The current relative velocity of the Character (see notes).

角色当前的相对速度(参见注解)。

This allows you to track how fast the character is actually walking for example when he is stuck at a wall this value will be the zero vector.

它允许你追踪角色究竟移动有多快,例如,当他被卡在墙壁里,这个值将变为0向量。

Note: The velocity returned is simply the difference in distance for the current timestep before and after a call to CharacterController.Move or CharacterController.SimpleMove. The velocity is relative because it won't track movements to the transform that happen outside of the CharacterController (e.g. character parented under another moving Transform, such as a moving vehicle).

注解:返回速度在调用CharacterController.Move或者CharacterController.SimpleMove之前和之后的时刻是不同的。速度是相对的,因为它不会追踪发生在CharacterController之外的变换的运动(例如,角色继承于另一个运动着的变换之下,比如一个运动着的车辆)。

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;
	}
}
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 
   //忽略Y轴的X-Z平面上的速度。
   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;
}
最后修改:2010年12月14日 Tuesday 13:31

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。