- Home /
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.
Answer by Benproductions1 · Jul 13, 2013 at 10:12 AM
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
@Benproductions1 Thanks a lot, it works but now my cube looks very buggy, when I stop moving, the cube sometimes slide an abnormal distance and rotates also. Any ideas what I do wrong? I posted my full code above.
@Benproductions1 btw, I use a cube as gameobject for the movement :) just so you know.
You might want to check if the magnitude of the non-normalized direction >
some arbitrarily small value. I'll edit my answer to include that ;)
@Benproductions1 thanks again for helping me out, unfortunately it doesn't seem to be working, still a bit buggy, could you test the code if it works for you? Would be awesome, since I really cant get it to work without part that the cube moves much longer than it should.
@kevinv321 Just FIY, rotation isn't affected by my script. I just tested it in an empty project and it works perfectly.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Moving your player to another location onTrigger 0 Answers
Should character direction come from key press, or third person controller vector3? 0 Answers
How do I set up my players controller script (How do I change the controls used to move and look) 2 Answers
Problem with player movements 1 Answer