Unity2D 人物移动切换人物图片

 
勾选Constraints_freeze Rotation_z轴锁定,防止碰撞偏移。

       public float moveSpeed = 3f;//定义移动速度
       private SpriteRenderer sr;//引用
       public Sprite[] tankSprite; //数组储存替换素材
       /// <summary>
       /// 初始化
       /// </summary>
       private void Awake()
       {
              sr = GetComponent<SpriteRenderer>();
       }
       /// <summary>
       /// 固定帧执行,防止抖动
       /// </summary>
       private void FixedUpdate()
       {
              float h = Input.GetAxisRaw("Horizontal");
              float v = Input.GetAxisRaw("Vertical");
              transform.Translate(Vector3.right * h * moveSpeed * Time.fixedDeltaTime, Space.World);//上下转向
              transform.Translate(Vector3.up * v * moveSpeed * Time.fixedDeltaTime, Space.World);//左右转向
        if (h<0)
              {
                     sr.sprite = tankSprite[3];//替换为图片4
              }//下转向
              else if (h>0)
              {
                     sr.sprite = tankSprite[1];//替换为图片2
              }//上转向
              if (v < 0)
              {
                     sr.sprite = tankSprite[2];//替换为图片3
              }//右转向
              else if (v > 0)
              {
                     sr.sprite = tankSprite[0];//替换为图片1
              }//左转向
       }
 
posted @ 2020-11-25 09:32  白兔兔秃~  阅读(572)  评论(0)    收藏  举报
/* */