【帅刺猬】Unity 3d 对象行动控制代码

//变量初始化,行动初始速度,体重,方向等

  1. private var walkSpeed : float = 1.0;   
  2. private var gravity  = 100.0;
  3. private var moveDirection : Vector3 = Vector3.zero;
  4. private var charController : CharacterController;
  5. //此方法中初始化对象
  6. function Start()
  7. {
  8.   charController = GetComponent(CharacterController);
  9.   animation.wrapMode = WrapMode.Loop;
  10. }
  11. //根据每一帧更新,进行动作
  12. function Update () 
  13. {
  14.   if(charController.isGrounded == true)
  15.   {
  16.     if(Input.GetAxis("Vertical") > .1)
  17.     {
  18.       if(Input.GetButton("Run"))
  19.       {
  20.         animation.CrossFade("run");
  21.         walkSpeed = 4;
  22.       }
  23.       else
  24.       {
  25.         animation["walk"].speed = 1;
  26.         animation.CrossFade("walk");
  27.         walkSpeed = 1;
  28.       }
  29.     }
  30.     else if(Input.GetAxis("Vertical") < -.1)
  31.     {
  32.       animation["walk"].speed = -1;
  33.       animation.CrossFade("walk");
  34.       walkSpeed = 1;
  35.     }
  36.     else
  37.     {
  38.       animation.CrossFade("idle");
  39.     }
  40.     
  41.     // Create an animation cycle for when the character is turning on the spot
  42.     if(Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical"))
  43.     {
  44.       animation.CrossFade("walk");
  45.     }
  46.     
  47.     
  48.     transform.eulerAngles.y += Input.GetAxis("Horizontal");
  49.     // Calculate the movement direction (forward motion)
  50.     moveDirection = Vector3(0,0, Input.GetAxis("Vertical"));
  51.     moveDirection = transform.TransformDirection(moveDirection);
  52.       
  53.   }
  54.   
  55.   moveDirection.y -= gravity * Time.deltaTime;
  56.   charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
  57. }
posted @ 2015-06-19 10:13  帅刺猬之家  阅读(341)  评论(0编辑  收藏  举报