zoukankan      html  css  js  c++  java
  • 【帅刺猬】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. }
    You can reach me by surfing the web ---- huntjobs.cn,or sending e-mails to me,Here is my qq MailBox:1424870395@qq.com
  • 相关阅读:
    Spring 声明式事务管理(11)
    Spring JdbcTemplate详解(9)
    Spring 基于xml配置方式的AOP(8)
    Spring AspectJ 切入点语法详解(7)
    Spring 基于Aspectj切面表达式(6)
    spring AOP 编程--AspectJ注解方式 (4)
    Spring 切面优先级(5)
    Spring 泛型依赖注入(3)
    python反射/自省 (目前是转载)
    flask_requirements
  • 原文地址:https://www.cnblogs.com/HedgehogBlog/p/4587824.html
Copyright © 2011-2022 走看看