Unity2D:使用UI按钮移动播放器-流畅的动画

时间:2018-09-08 22:10:36

标签: c# unity3d

我有两个UI按钮,用于控制播放器的移动(左右),而不是使用按钮脚本组件,而是决定使用事件触发脚本组件。下移事件以移动播放器,而上移事件则停止播放器的移动。我的问题:我创建了一个步行动画(在动画师内部),当用户按住左键或右键时,该动画会播放(我喜欢),但该动画很流畅,但是如果玩家点击按下一次按钮(而不是按住按钮),动画将不播放,移动也不会移动,或者动画太快且移动非常短。我想做的是:我想保持播放器的运动状态,但我也希望在用户轻按一次时为播放器提供流畅的动画和运动。

这是我的脚本:

public bool movingLeft = false;
public bool movingRight = false;

public float speed = 2f;

public Vector3 moveDirectionLeft = Vector3.left;
public Vector3 moveDirectionRight = Vector3.right;


 void Start () {
    movingLeft = false;
    movingRight = false;

  }

 void Update () {
     if (movingLeft == true) {                      // LEFT BUTTON //
        WalkAnim.SetBool ("WalkLeft", true);// walk left
            transform.Translate (moveDirectionLeft * speed * Time.deltaTime);
     }  else if (movingLeft == false) {
           speed = 0;
           WalkAnim.SetBool ("WalkLeft", false); // goes back to idle
     }


     if (movingRight == true) {                         // RIGHT BUTTON //
        WalkAnim.SetBool ("WalkRight", true); // walks right
            transform.Translate (moveDirectionRight * speed * Time.deltaTime);
     }  else if (movingLeft == false) {
           speed = 0;
           WalkAnim.SetBool ("WalkRight", false); // goes back to idle
     }
 }


public void limitLD() { // UI Button Event trigger - pointUp
    movingLeft = false;
}

public void limitRD() { // UI Button Event trigger - pointUp
    movingRight = false;
}


public void MoveLeft() { // UI Button Event trigger - pointDown
        movingLeft = true;
        movingRight = false;
}

public void MoveRight() { // UI Button Event trigger - pointDown
        movingLeft = false;
        movingRight = true;
}

1 个答案:

答案 0 :(得分:1)

由于您的if...else语句而发生此问题。例如,如果您单击Right按钮,则会调用void MoveRight()。它设置了movingRight = truemovingLeft = false

因此,在您的Update()函数中,首先运行else if (movingLeft == false)并将速度设置为0,并将动画设置为空闲。第二个if (movingRight == true)运行。但是因为speed = 0animation = idleUpdate()函数调用了每一帧,所以什么也没发生。

您可以将脚本更改为此:

public bool movingLeft = false;
public bool movingRight = false;

public float speed = 2f;

public Vector3 moveDirectionLeft = Vector3.left;
public Vector3 moveDirectionRight = Vector3.right;


void Start()
{
    movingLeft = false;
    movingRight = false;
}

void Update() 
{
    if (movingLeft == true)
    {                      // LEFT BUTTON //
       WalkAnim.SetBool("WalkLeft", true);// walk left
       transform.Translate(moveDirectionLeft * speed * Time.deltaTime);
    }
    else if (movingRight == true)
    {                         // RIGHT BUTTON //
       WalkAnim.SetBool("WalkRight", true); // walks right
       transform.Translate(moveDirectionRight * speed * Time.deltaTime);
    }
    else
    {
       WalkAnim.SetBool("WalkLeft", false);
       WalkAnim.SetBool("WalkRight", false);
    }
}

public void limitLD()
{ // UI Button Event trigger - pointUp
    movingLeft = false;
}

public void limitRD()
{ // UI Button Event trigger - pointUp
    movingRight = false;
}

public void MoveLeft()
{ // UI Button Event trigger - pointDown
    movingLeft = true;
    movingRight = false;
}

public void MoveRight()
{ // UI Button Event trigger - pointDown
    movingLeft = false;
    movingRight = true;
}

希望对您有帮助。