How to make diagonal keyboard input the same as controller?

Sorry for the noob question, I’ve been learning unity(2D) / coding for 3 days or so.

I have this simple code to get the input direction and do a dash in whatever direction the user is holding at the time of pressing dash. I’m using different x and y dash speeds intentionally btw.

xRawInput = Input.GetAxisRaw("Horizontal");
yRawInput = Input.GetAxisRaw("Vertical");

if (pressedDash) {
            rb.velocity = Vector2.zero;
            rb.velocity += new Vector2(xRawInput * xDashSpd, yRawInput * yDashSpd);
            Invoke("setDashToFalse", dashDuration);
        }

My problem is, if I hold diagonal (W and D for example) on keyboard, I dash a lot further than if I hold diagonal on a joystick. I think I know why this is, because W and D are both returning 1 inside the RawInput’s, whereas diagonal on a joystick returns something like 0.7. How can I make it so both inputs give the same results? any help is greatly appreciated, ty.

You’ll need to normalise your vector before passing it to your rigidbody’s velocity: Unity - Scripting API: Vector2.Normalize

This ensures the vector only has a magnitude of 1.

You can see good information on this in CatLikeCoding’s series about movement: Sliding a Sphere

1 Like