Hello, in my script, i have a problem with my caracter speed, when i move he foward, the speed is very high, but, i don´t now why, because my caracter speed is 1.
What is wrong here?
#pragma strict
//Inspector Variables
var playerSpeed : float = 1; //speed player moves
var turnSpeed : float = 100; // speed player turns
function Update ()
{
MoveForward(); // Player Movement
TurnRightAndLeft();//Player Turning
}
function MoveForward()
{
if(Input.GetKey("up"))//Press up arrow key to move forward on the Y AXIS
{
transform.Translate(1,playerSpeed * Time.deltaTime,0);
}
}
function TurnRightAndLeft()
{
if(Input.GetKey("right"))
{
transform.Rotate(-Vector3.forward *turnSpeed* Time.deltaTime);
}
if(Input.GetKey("left"))
{
transform.Rotate(Vector3.forward *turnSpeed* Time.deltaTime);
}
}
Transform.Translate adds to the current object position the amount of units given in the translation vector (transform.translate(1, 1, 1) ) will move your object 1 unit in each direction.
Time.deltaTime is the amount of time that has passed from the last frame. Mine is around 0.015.
When you translate your object you move it on y with around (1 * 0.015) units every frame. Now to test it you can place an object to (0, 0, 0) then to (0, 0.015, 0) and see how much did it move, and think that every frame your object will move that amount. Now think how many frames are on average in one second, and multiply that with 0.015, and you will get units per second. You have to judge if that is fast for you, but I think it’s pretty fast.
Please take into account that my calculations are an aproximation, and Time.deltaTime does not have a static value, nor does it always surrounds the value 0.015. Also because you are using Time.deltaTime your object speed is the same on all frame per second.
Just Debug.log(Time.deltaTime) on Update make an average and test it with your values