Hello!
I do not want the player walks diagonally.
How do I prevent this using this code?
public class Moviment : MonoBehaviour
{
private float speedLocomotion = 2.5f;
public Rigidbody2D player;
void Update()
{
float x = Input.GetAxisRaw("Horizontal");
float y = Input.GetAxisRaw("Vertical");
Vector2 direction = new Vector2(x, y).normalized;
player.velocity = direction * speedLocomotion;
}
}
Your problem is going to be that you are putting both the x and the y into the vector. Of course, it will be diagonal if you do that, as x and y are both going to possibly be different from zero. What you need to do it apply one, or the other, but not both. I would use an if statement, decide if x is bigger or y is bigger, and then apply that one, only that one. By adding the if statement, you allow both axes to be moved, and despite that, only move on one of them.
Well observed kburkhart thank you!
I solved my problem using this code.
public class Moviment : MonoBehaviour
{
public Rigidbody2D player;
private Vector2 speedPlayerX;
private Vector2 speedPlayerY;
void Start()
{
player = GetComponent<Rigidbody2D>();
speedPlayerX = new Vector2(2, 0); // move at position X
speedPlayerY = new Vector2(0, 2); // move at position Y
}
void Update()
{
//Mount the condition before inserting this code
if (Input.GetKey("w"))
{
player.MovePosition(player.position + speedPlayerY * Time.deltaTime); // Move Up
}
if (Input.GetKey("s"))
{
player.MovePosition(player.position + -speedPlayerY * Time.deltaTime); // Move Down
}
if (Input.GetKey("a"))
{
player.MovePosition(player.position + -speedPlayerX * Time.deltaTime); // Move Left
}
if (Input.GetKey("d"))
{
player.MovePosition(player.position + speedPlayerX * Time.deltaTime); // Move Right
}
}
}
}
Hmm, you appear to still have the same problem. Unless you want to allow diagonal movement, you need a change there. If you press ‘w’ and ‘d’ at once, it will move diagonal. If that is what you want, OK, but if not, you need to set a variable or do something similar so that once you have either a right or left movement, you don’t allow the up/down, or the other way around.
This would stop moving object when both axis / buttons are pressed, and I’d say that this is much better solution for your problem:
float x = Input.GetAxisRaw("Horizontal");
float y = Input.GetAxisRaw("Vertical");
int isDiagonal = x * y != 0 ? 0 : 1;
transform.position += new Vector3(x, y, 0) * isDiagonal * Time.deltaTime;
The issue I see with that method is that it totally stops movement. I believe the OP still wants movement, but only in whichever direction is more prominent(more pressed down). Of course, if we had more information on the exact needs, we could be better at giving the answer.
My code is hughe I know! but my code also works in case you want to make your character keep walking in the last direction he decided to walk…
example: you press d and your char walks right then you simultaneously press w or a and he keeps walking right…
this code is good bc you dont lock your char to either of the axes and also because you can make your character keep moving instead of stoping him from move!