user1145909
user1145909

Reputation:

Unity Diagonal Movement Not Working

I want my player to move in diagonal directions and this is the code I am using to move in a down diagonal way:

 if (Input.GetAxisRaw("Horizontal") > 0f && Input.GetAxisRaw("Vertical") < 0f)
 {
      front45 = true;
      rb.velocity = new Vector3(moveSpeed, -moveSpeed, 0f);
 }

however the rigidbody2d wont move in that direction. It will move up, down and from side to side, but never diagonal.

The front45 = true is just for the animator to know when to change animation.

Upvotes: 0

Views: 555

Answers (1)

Agustin0987
Agustin0987

Reputation: 601

I would try something like this:

float h = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
float v = Input.GetAxisRaw("Vertical") * Time.deltaTime;

if (h != 0 && v != 0)
    front45 = true; //Not sure what this does, so I just left it inside the condition

rb.velocity = new Vector3(h * moveSpeed, v * moveSpeed, 0f);

This should work on any direction.

Upvotes: 1

Related Questions