Or rather I should say, “EFFECTIVELY disabling diagonal movement”.
There are plenty of Q/As online about this, but I keep encountering the same problem: When moving horizontally (left, for example), I can override the current direction and start moving vertically (by pushing up), which is what I want. But it does not work the other way around! Vertical movement can not override horizontal movement.
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
ManageMovement(h, v);
}
void ManageMovement(float horizontal,float vertical) {
if (vertical != 0f) {
horizontal = 0f;
Vector3 movement = new Vector3 (horizontal, vertical, 0);
GetComponent<Rigidbody2D> ().velocity = movement * speed;
return;
}
if (horizontal != 0f) {
vertical = 0f;
Vector3 movement = new Vector3 (horizontal, vertical, 0);
GetComponent<Rigidbody2D> ().velocity = movement * speed;
return;
} else {
Vector3 noMovement = new Vector3 (0, 0, 0);
GetComponent<Rigidbody2D> ().velocity = noMovement;
}
}
If I reverse the order of these if() statements, it reverses the problem. So, that’s a clue. But I’m not a great detective. I would love some help!