【帅刺猬】Unity 3d 对象行动控制代码
//变量初始化,行动初始速度,体重,方向等
-
private var walkSpeed : float = 1.0;
-
private var gravity = 100.0;
-
private var moveDirection : Vector3 = Vector3.zero;
-
private var charController : CharacterController;
-
//此方法中初始化对象
-
function Start()
-
{
-
charController = GetComponent(CharacterController);
-
animation.wrapMode = WrapMode.Loop;
-
}
-
//根据每一帧更新,进行动作
-
function Update ()
-
{
-
if(charController.isGrounded == true)
-
{
-
if(Input.GetAxis("Vertical") > .1)
-
{
-
if(Input.GetButton("Run"))
-
{
-
animation.CrossFade("run");
-
walkSpeed = 4;
-
}
-
else
-
{
-
animation["walk"].speed = 1;
-
animation.CrossFade("walk");
-
walkSpeed = 1;
-
}
-
}
-
else if(Input.GetAxis("Vertical") < -.1)
-
{
-
animation["walk"].speed = -1;
-
animation.CrossFade("walk");
-
walkSpeed = 1;
-
}
-
else
-
{
-
animation.CrossFade("idle");
-
}
-
-
// Create an animation cycle for when the character is turning on the spot
-
if(Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical"))
-
{
-
animation.CrossFade("walk");
-
}
-
-
-
transform.eulerAngles.y += Input.GetAxis("Horizontal");
-
// Calculate the movement direction (forward motion)
-
moveDirection = Vector3(0,0, Input.GetAxis("Vertical"));
-
moveDirection = transform.TransformDirection(moveDirection);
-
-
}
-
-
moveDirection.y -= gravity * Time.deltaTime;
-
charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
-
}
You can reach me by surfing the web ---- huntjobs.cn,or sending e-mails to me,Here is my qq MailBox:1424870395@qq.com