My obj moves faster when z and x movement

Hi All,

I have a basic movement script which looks like:

h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
Vector3 v3_player_movement = new Vector3(h * movementSpeed * Time.deltaTime, 0f, v * movementSpeed * Time.deltaTime);
transform.Translate(v3_player_movement, Space.World);

When I press 1 key for movement, its looks very good, but when I press 2 buttons, like up and left. The movement of my cube seems faster (more distance each second), I think this is because up + left movement will cause in a diagonal movement which you will do more distance in each second. Unfortunately I have no idea how to limit the speed so that up + left has the same distance each second as up or left single pressed?

UPDATE FULL CODE:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
	public float turnSmoothing = 15f;
	public float movementSpeed = 0.5f;
	
	private float debug_time = 0f;
	private Vector3 last_spotted;
	private float h;
	private float v;
	
	void Update() {
		h = Input.GetAxis("Horizontal");
		v = Input.GetAxis("Vertical");
		MovementManagement(h, v);
	}
	
	void MovementManagement(float h, float v) {
		if (h != 0f || v != 0f) {
		  Rotating(h, v);
		  Move(h, v);
		}
	}
	
	void Move(float h, float v) {
		// Calculate the direction
		Vector3 direction = new Vector3(h, 0f, v).normalized;
		// Calculate velocity
		Vector3 velocity = direction * movementSpeed * Time.deltaTime;
		// apply
		transform.Translate(velocity, Space.World);
	}
	
	void Rotating(float h, float v) {
		Vector3 targetDirection = new Vector3(h, 0f, v);
		Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
		Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
		rigidbody.MoveRotation(newRotation);
	}
}

Thanks, all help is welcome.

Hello,

What you are doing for movement, is you are separating movement into two axis(s), so it’s easier to apply the separated input.
If you want to only move in one direction at a specified speed (and only at that speed), instead you should calculate the direction (as a vector) and them multiply it by the speed:

float movementSpeed = 0.5f;

//Get input
float hor = Input.GetAxis("Horizontal");
float vert = Input.GetAxis("Vertical");

//Calculate the direction
//If you are unsure what "normalized" does
//I suggest learning how vectors work
Vector3 direction = new Vector3(hor, 0f, vert);

//only move when we don't have a very small Input value
if (direction.magnitude > 0.001) {
    direction.Normalize()
}
else {
    return;
}

//Calculate Velocity from direction
//And move
Vector3 velocity = direction * movementSpeed * Time.deltaTime;
transform.Translate(velocity, Space.World);

The code is untested, but should work. (I made sure to try to keep it #pragma strict)

Hope it helps,
Benproductions1