- Home /
Avoiding 2D top-down diagonal movement
Hello Everyone,
I'm working on a 2D game and I'm trying to figure how how to prevent diagonal movement. I've succeeded in preventing the character from moving diagonally, but I have a weird issue.
When I'm moving along the x axis and input to move along the y axis, it works as intended and stops moving horizontally and starts moving vertically. However, when I'm moving along the y axis and try to move horizontally, he will just continue moving up. I'm not entirely sure what's wrong.
public class Controls : MonoBehaviour {
public float speed = 5;
public Rigidbody2D rb;
public Vector2 movement;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if (movement.y != 0)
{
movement.x = 0;
}
if (movement.x != 0)
{
movement.y = 0;
}
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * speed * Time.deltaTime);
}
}
Firstly, you already asked a similar question:
https://answers.unity.com/questions/1839503/2d-rigidbody-top-down-movement-problem.html
The answer will be similar.
Secondly, it is incorrect to use deltaTime in FixedUpdate. Correctly use fixedDeltaTime.
Answer by Artik2442 · Jun 01, 2021 at 11:20 AM
I already answered at that question but I guess I'll rewrite it.
When your script is running, when y axis is used, you set x axis movement to zero. So, when you press a key that will change x axis, nothing happens (just you keep going on Y axis). To prevent this, you have to check what was the last Axis used by your Keys. So if it was W or S it was Y axis, and else, it was X axis. What I recommend is to check if the value of Horizontal or Vertical have changed. So:
public class Controls : MonoBehaviour
{
public Vector2 movement;
void Update()
{
float x = Input.GetAxisRaw("Horizontal");
float y = Input.GetAxisRaw("Vertical");
float lastX; //To check if current x value has changed from last
float lastY; //To check if current y value has changed from last
string LastKeyUsed = ""; //will know which axis was changed
if(x!=lastX) //if current x value has changed, we set the last used axis to X and set lastX to current x value
{
LastKeyUsed = "X";
lastX = x;
}
if(y!=lastY) //if current y value has changed, we set the last used axis to Y and set lastY to current y value
{
LastKeyUsed = "Y";
lastY = y;
}
if(x != 0 && y== 0) //In case you stop using the last key and have still an other key pressed, you come back to the other key axis
{
LastKeyUsed = "X"
}
if(x == 0 && y!= 0)
{
LastKeyUsed = "Y"
}
if(LastKeyUsed == "X") //Then we check which axis was last used, and we set movement x and y value depending on the result
{
movement.x = x;
movement.y = 0;
}
else if(LastKeyUsed == "Y")
{
movement.x = 0;
movement.y = y;
}
}
}
And voilà! Just keep your FixedUpdate as it is. If any help needed, im here. It might not work, but I hope you got the idea.
Answer by Tsuki92 · Apr 21 at 12:57 PM
public float moveSpeed = 5f;
float lastX; //To check if current x value has changed from last
float lastY; //To check if current y value has changed from last
string LastKeyUsed = ""; //will know which axis was changed
public Rigidbody2D rb;
public Animator anim;
Vector2 movement;
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
anim.SetFloat("Speed", movement.sqrMagnitude);
}
private void FixedUpdate()
{
if (movement.x > 0.01f || movement.x < -0.01f)
{
movement.y = 0;
anim.SetFloat("Horizontal", movement.x);
anim.SetFloat("Vertical", movement.y);
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
else if ( movement.y > 0.01f || movement.y < -0.01f)
{
movement.x = 0;
anim.SetFloat("Horizontal", movement.x);
anim.SetFloat("Vertical", movement.y);
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
Try this code!, just ignore the parts about animation if you are not using a blend tree.
The code i submitted above locks you from going up while you have left key down.
If you want it to swap between them but still no diagonal rotation just move the code to update() instead of FixedUpdate()
Your answer
Follow this Question
Related Questions
How to turn off gravity? 4 Answers
Rigidbody2D drifitng after collision 2 Answers
How to close an open door? 1 Answer
2d controll rigidboy.velocity.y 1 Answer
Blocking object from going through colliders (no rigidbody) [2D] 1 Answer