Unity2D游戏开发 ——主角移动时播放相应动画

一 动画状态机设置:动画

1.blog

2.ci

3.it

4.class

5.date

二 上代码:im

   private Animator anim;  //动画组件d3

    void Start()
    {
        rig = GetComponent<Rigidbody2D>();   //获取主角刚体组件
        anim = GetComponent<Animator>();
    }db

void Update()
    {img

       horizontal = Input.GetAxis("Horizontal");   //水平方向按键偏移量
        move = horizontal * moveSpeed;   //刚体具体速度
        rig.velocity = new Vector2(move, rig.velocity.y);

        if(horizontal >0)                  // 播放向右走动画         {             anim.SetBool("IsRight", true);             anim.SetBool("IsLeft", false);         }         else if(horizontal < 0)         // 播放向左走动画         {             anim.SetBool("IsLeft", true);             anim.SetBool("IsRight", false);         }         else                                 //静止 Idle 动画         {             anim.SetBool("IsRight", false);             anim.SetBool("IsLeft", false);         }             } }