0
votes

I have a two UI buttons that I use to control my player's movement (left and right), instead of using the button script component I decided to an event trigger script component; a point down event to move my player and the point up event to stop the movement of my player. My issue: I created a walking animation (within my animator) that will play when the user press and hold either the left or the right button, the animation is smooth (which is what I like) but if the player were to tap on the button once (rather than tap and holding the button) the animation doesn't play and movement is not moving or the animation is too quick and the movement is really short. What I want to do: I want to keep how the player movement is but I also want a smooth animation and movement for the player when the user taps once.

This is my script:

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

1 Answers

1
votes

This problem occurs because of your if...else statements. For example, if you click on the Right button, void MoveRight() called. It set movingRight = true and movingLeft = false.

So in your Update() function, first else if (movingLeft == false) runs and set your speed to 0 and set your animation to idle. and second if (movingRight == true) runs. But because speed = 0 and animation = idle and Update() function called every frame, nothing happened.

You can change your script to this:

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;
}

I hope it helps you.