# (3d) Moving diagonally is faster?

I know I know, it’s been asked a million times, but I’ve literally spent 3 days scouring the internet and I can’t find a definitive solution, so here goes…

I have a kinematic gameobject I’m trying to move on the x,z plane with WASD (or arrow keys) relative to player orientation.

I’ve set speed to 10 in Unity, and moving in a straight line produces exactly 10 velocity when logged. The problem (I think…?) is with diagonal movement. I’ve normalized the vector prior to multiplying speed and deltaTime, but if you add the forward and lateral move speeds during diagonal movement it still comes to about 40% faster than moving in a single direction.

Here’s the script, followed by a sample line from the console while both ‘w’ and ‘d’ were pressed:

``````using UnityEngine;

public class PlayerController : MonoBehaviour
{

public float speed;

private Rigidbody rigidBody;
private Vector2 input;

private void Awake()
{
rigidBody = GetComponent<Rigidbody>();
}

void Update()
{
input = new Vector2();
if (Input.GetKey(KeyCode.A)) { input.x = -1; }
if (Input.GetKey(KeyCode.D)) { input.x = 1; }
if (Input.GetKey(KeyCode.W)) { input.y = 1; }
if (Input.GetKey(KeyCode.S)) { input.y = -1; }
}

private void FixedUpdate()
{
Vector3 movement = transform.right * input.x;
movement += transform.forward * input.y;
movement.Normalize();

movement *= speed * Time.fixedDeltaTime;

rigidBody.MovePosition(rigidBody.position + movement);

Vector3 myVelocity = rigidBody.velocity;
Debug.Log(myVelocity);
}
}
``````
``````(7.1, 0.0, 7.1)
UnityEngine.Debug:Log(Object)
``````

I am admittedly fairly new to programming, very new to Unity, and not especially strong in physics lol. Any feedback or advice would be greatly appreciated!

• a fellow game lover tryin’ to make one
1 Like

Everything is correct. I can’t see any 40% speed increment in your code nor in the debug output. Doing the math with [7.1, 0.0, 7.1] results in a speed of 10 m/s. The calculation with those numbers is ~10.04, but that’s because the vector string shows one decimal only by default.

Why don’t you simply log the velocity magnitude? That’s the actual speed of movement.

``````Debug.Log(myVelocity.magnitude)
``````