首页 > 代码库 > unity3d控制模型的运动

unity3d控制模型的运动

这里就不多做解释了,直接上代码,只为了备忘。

public class HeroMove : MonoBehaviour {

    private float speed;//人物行动速度
    private Animation ani;

    // Use this for initialization
    void Start () {
        speed = 1f;
        ani = GetComponent<Animation> ();
    }
    
    // Update is called once per frame
    void Update () {
        /*向前走/跑*/
        if (Input.GetKey (KeyCode.W)) {
            transform.Translate (Vector3.forward * Time.deltaTime * speed);
            if (Input.GetKey (KeyCode.LeftShift)) {
                speed = 3f;
                ani.Play ("Run");
            } else {
                speed = 1f;
                ani ["Walk"].speed = 1;
                ani.Play ("Walk");
            }
        } else if (Input.GetKey (KeyCode.S)) {
            //向后退
            transform.Translate (Vector3.back * Time.deltaTime * speed);
            ani ["Walk"].speed = -1;
            ani.Play ("Walk");
        } else {
            //停止
            ani.Play ("Idle");
        }
    }
}

 

unity3d控制模型的运动