# How do I prevent my object from doubling speed when I press two keys

Basically if I press keys such as ‘W,D’ ‘W,A’ ‘S,A’ ‘S,D’ it doubles the speed of my object and I simply don’t want that so if there are any tweaks to the code you suggest please advise me!

``````using UnityEngine;

public class Player_Movement : MonoBehaviour
{
public float speed = 5f;

// Update is called once per frame
void FixedUpdate()
{
MoveUp();
MoveDown();
MoveRight();
MoveLeft();
Sprint();

}

void MoveUp ()
{
if (Input.GetKey("w"))
{
transform.Translate(speed * Time.deltaTime, 0, 0);

}

}

void MoveDown()
{
if (Input.GetKey("s"))
{
transform.Translate(-speed * Time.deltaTime, 0, 0);

}

}

void MoveLeft()
{
if (Input.GetKey("a"))
{
transform.Translate(0, 0,speed * Time.deltaTime);

}

}

void MoveRight()
{
if (Input.GetKey("d"))
{
transform.Translate(0, 0,-speed * Time.deltaTime);

}

}

public void Sprint()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{

speed = 10f;

}
else if (Input.GetKeyUp(KeyCode.LeftShift) )
{
speed = 5f;

}
}

}
``````

You need to combine the movement to a vector and normalize it’s length. This should help you on the way.

``````Vector3 v = new Vector3(0, 0, 0);
void FixedUpdate()
{
Sprint();
v.x = v.z = 0;
if (Input.GetKey("w")) v.x += 1;
if (Input.GetKey("s")) v.x -= 1;
if (Input.GetKey("a")) v.z += 1;
if (Input.GetKey("d")) v.z -= 1;
if (v.sqrMagnitude > 0.1f) v.Normalize();
v *= speed * Time.deltaTime;
transform.Translate(v);
}
``````