thrashing moose
thrashing moose

Reputation: 25

Unity game touch button - object only moves right, not left

Using the UI canvas button, I am attempting to move my object left and right with pointer down, and on pointer up, the movement should stop. However only the switch case to move right executes, and my object does not move left (it will print the statements however)

This code is attached to the left button. On pointer down MoveLeft() is called, on pointer up, NoLeft() is called (through the inspector using event trigger). The boolean isLeft controls whether the left movement will occur or not.

public class LeftButton : MonoBehaviour {

public GameObject playerC;

public void MoveLeft(){
    Debug.Log ("Moving left");
    playerC.GetComponent<PlayerController>().isLeft = true;

}

public void NoLeft(){
    Debug.Log ("Not moving left");
    playerC.GetComponent<PlayerController> ().isLeft = false;
}
}

The code below is attached to the player, this is where the problem lies I suspect, I can only move right. The log statements for isLeft will print however.

public class PlayerController : MonoBehaviour {

private Rigidbody playerRigidBody;
[SerializeField]
public float movementSpeed;

public bool isLeft;
public bool isRight;


void Start () {

    playerRigidBody = GetComponent<Rigidbody> ();
}

void FixedUpdate () {

    switch (isLeft) {
    case true:

        print ("Move left is true");
        playerRigidBody.MovePosition(transform.position + transform.forward * 0.5f);
        break;

    case false:

        print ("No longer left");
        playerRigidBody.MovePosition (transform.position + transform.forward * 0f);
        break;

    }

    switch (isRight) {
    case true:

        print ("Move right is true");
        playerRigidBody.MovePosition (transform.position - transform.forward * 0.5f);
        break;

    case false:

        print ("No longer right");
        playerRigidBody.MovePosition (transform.position - transform.forward * 0);
        break;

    }

}

The statement 'No longer right' also prints even if I never touch the right button and release it.

In case you are wondering the UI is made up of two buttons, Left and Right. They both have their own scripts LeftButton (above) and RightButton that just mirror each other.

Thanks in advance for any help.

Upvotes: 0

Views: 652

Answers (1)

Everts
Everts

Reputation: 10701

You are overcomplicating it and this is where it goes wrong. Simply have a method that takes a float value which is either positive or negative. In your situation, isLeft and isRight are always true or false. So the FixedUpdate runs, it will run both switch and prints the matching state.

public class PlayerController : MonoBehaviour 
{
    public void Move(float polarity) {
        playerRigidBody.MovePosition(transform.position + transform.forward * polarity);
    }
}

Move is the method you assign to both buttons and then give the polarity (1 or -1) to the inspector.

Upvotes: 1

Related Questions