Diagonally movement too fast (top-down)

Hi,

I’m making a top-down shooter to learn Unity. My character moves with rigidbody and I’m trying to limit (normalize?) the diagonally movement speed. How am I suppose to do it with this script:

//Left

if (Input.GetKey ("a")){

	rigidbody.AddForce(Vector3.right * -moveSpeed * Time.deltaTime);
	}
//Right
if(Input.GetKey("d")){

	rigidbody.AddForce(Vector3.right * moveSpeed * Time.deltaTime);
	}
//Up

if (Input.GetKey ("w")){

	rigidbody.AddForce(Vector3.forward * moveSpeed * Time.deltaTime);
	
	}
//Down

if(Input.GetKey ("s")){

	rigidbody.AddForce(Vector3.forward * -moveSpeed * Time.deltaTime);
}

if its too fast

try adjusting the value of moveSpeed

The problem is when two buttons are being pressed you end up with a vector something like (1,1,0), which is longer that the single button options which will have components something like (1,0,0). One way to do this is to let the buttons change your direction vector then add the force separately. Try:

Vector3 direction = new Vector3(0,0,0);
//Left

if (Input.GetKey ("a")){
    direction -= Vector3.right;
    }

//Right if(Input.GetKey("d")){
   direction += Vector3.right;
    }

//Up

if (Input.GetKey ("w")){
    direction += Vector3.forward;     
    }

//Down

if(Input.GetKey ("s")){
    direction -= Vector3.forward;
}
if (direction != Vector3(0,0,0)
      rigidbody.AddForce(direction.normalized*moveSpeed*Time.deltaTime;

That will let you set the direction with each keypress, and then if the vector is not zero it will add the force in the specified direction. The ‘.normalized’ specifies that no matter the direction, its magnitude will alsways be 1. Should keep your speed constant.