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.