How do I fix this code?

So I’m making a top down shooter game on Unity, and it’s going well so far, but a problem that I have run into is that my player’s movement is based off of it’s rotation, so when my code to make my character face the mouse cursor is active, the controls get weird because of the direction that it is facing. The code I have currently for movement is below:

public float verticalInput;
 public float horizontalInput;
 public float speed;

 void Update()
 {
     horizontalInput = Input.GetAxis("Horizontal");
     verticalInput = Input.GetAxis("Vertical");

     Vector2 movement = new Vector2(horizontalInput, verticalInput).normalized;
     transform.Translate(movement * Time.deltaTime * speed);
}
transform.Translate(movement * Time.deltaTime * speed, Space.World);
1 Like

Thank you so much, I’ll test this later

public float verticalInput;
public float horizontalInput;
public float speed;

void Update()
{
    horizontalInput = Input.GetAxis("Horizontal");
    verticalInput = Input.GetAxis("Vertical");

    // Get the player's current rotation in radians
    float rotationInRadians = transform.rotation.eulerAngles.y * Mathf.Deg2Rad;

    // Calculate the movement vector based on the input and rotation
    Vector2 movement = new Vector2(horizontalInput * Mathf.Cos(rotationInRadians) - verticalInput * Mathf.Sin(rotationInRadians),
                                   horizontalInput * Mathf.Sin(rotationInRadians) + verticalInput * Mathf.Cos(rotationInRadians));

    transform.Translate(movement * Time.deltaTime * speed);
}
1 Like

Thank you so much, this really helped