# Diagonal movement is faster than horizontal or vertical movement. How to fix?

When using Horizontal or Vertical input keys, the the movement is normal. But when trying to move diagonal, multiple keys are to be pressed, and the speed just stacks up.

Yes there are many answers to such questions. But I'm having a hard time figuring out a solution. I am actually using an asset, and the code is kinda hard to understand for me. The script is on my drive and the link is below: https://drive.google.com/open?id=1Lt4DZBw7Jv2LNyYR-03aNUpV2a29F7dm

Please provide a solution. I am not so good at coding. And I'm in real need to make that game because of many reasons as fast as I can so I will learn all coding later. (and in the script please search for Move, Movement or related words to go to specific movement code, thank you a lot)

• Without looking at your code I can guess, that it has something along the lines of "if the left key is pressed, then set velocity.x to speed, if the forward key is pressed, then set velocity.y to speed". This means that the velocity when both key is pressed is √2 * speed – Bálint Jan 6 '18 at 14:37
• the script is just so much hard for me to understand. really, im unable to do it.its just so complex after looking at the code :'( – Prakash Jan 6 '18 at 14:52
• oh and yes Balint you are right. Its that maybe. But Im actually willing to get a solution based on the script :( – Prakash Jan 6 '18 at 14:56
• Include the relevant code in the question itself, so it can't be lost to link rot. – DMGregory Jan 6 '18 at 15:03
• If the code does not fit, then you have not done the work of creating a minimal, complete, verifiable example, which we require for questions involving debugging code. The type of feature you're describing can be summarized in a dozen lines or less. If you're unwilling to take the time to isolate that information from that giant linked script, why would anyone else? If you don't know how, I recommend throwing out that script and starting with something much simpler. – DMGregory Jan 6 '18 at 19:10

Try setting velocity to your desired velocity if both left and up (or right and down, etc) are pressed at the same time. That should do it.

• what if right down and left are pressed at the same time? – Wietlol Jan 6 '18 at 15:50
• @Wietlol you may not want to register movement for that. Or you may want to pick the last two pressed. – nerk Jan 6 '18 at 15:51
• depends on how you do the user input, still, the best approach by far is getting axes values (-1 to 1) for x movement and y movement and figuring out the direction (in radians), then multiply the speed by the sin and cos to get the actual movement, if both axis values are close to 0 (not equal, just close), you dont apply the movement – Wietlol Jan 6 '18 at 15:59
• please see this part of the script /// Moves the character according to the input. /// <param name="horizontalMovement">-1 to 1 value specifying the amount of horizontal movement.</param> /// <param name="forwardMovement">-1 to 1 value specifying the amount of forward movement.</param> public void Move(float horizontalMovement, float forwardMovement, Quaternion lookRotation) { // Store the velocity as it will be used by many of the functions below. m_Velocity = m_Rigidbody.velocity; // Store the input parameters. m_InputVector.x = horizontalMovement; m_InputVector.z = forwardMovement; – Prakash Jan 6 '18 at 16:40

If Unity3D:

this._inputs = Vector3.zero;
this._inputs.x = Input.GetAxis("Horizontal");
this._inputs.z = Input.GetAxis("Vertical");
if(this._inputs.magnitude > 1)
this._inputs = this._inputs.normalized;

• You can also use Vector3.ClampMagnitude to achieve this effect. And note that you usually won't need all these leading "this." keywords. It's implicit if there's not a local variable/argument shadowing the same name. – DMGregory Jan 23 '19 at 20:23