charactercontroller.velocity 速度

2015-1-4 12:07| 发布者: 杨炎| 查看: 2250| 评论: 0|原作者: 蛮牛|来自: unity3d脚本manual

摘要: charactercontroller.velocity 速度varvelocity:vector3description描述the current relative velocity of the character (see notes).角色当前的相对速度(参见注解)。this allows you to track how fast the char ...

charactercontroller.velocity 速度



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.


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).




using unityengine;

using system.collections;

public class example :monobehaviour{

void update() {

charactercontroller controller = getcomponent();

vector3 horizontalvelocity = controller.velocity;

horizontalvelocity = newvector3(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


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;