This is one of my first coding projects, so apologies for beginner questions....
My player controller script uses the below code, but I want to limit movement to horizontal and vertical only. No diagonal movement.
Here's how it looks:
public class PlayerController : MonoBehaviour
{
//I’m moving the rigid body to move the player
public Rigidbody2D theRB;
//lets me change player's move speed in unity editor
public float moveSpeed;
// Update is called once per frame
void Update()
{
theRB.velocity = new Vector2( Input.GetAxisRaw("Horizontal")
, Input.GetAxisRaw("Vertical")) * moveSpeed;
I've tried the below code to only accept one command at a time by using an if statement, however, with this new code, the player doesn't stop walking once the key is no longer pressed.
Just wondering if someone could point me in the direction of what I'm doing wrong?
// Update is called once per frame
void Update()
{
if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
{
theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * moveSpeed;
}
if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * moveSpeed;
}
thanks!