var speed : float = 5.0;
var rotationSpeed : float = 50.0;
function Update () {
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
if(Input.GetKeyDown(KeyCode.LeftShift)){ // Problem is here obviously.
speed = speed + 10;
}
}
Its something with the left shift.
I think its just doesnt stop adding speed.
I don’t know why it crashes but the problem is not in the GetKeyDown block
Well, when I run it I can walk around properly but when I press the sprint button my game freezes.
So no error appears, it just freezes?
speed = 10;
not
speed = speed + 10;
also, once you start running, you will never be able to slow down again… This could be the problem, as well as a logic flaw
Or speed += 10; that will add ten to speed.
No. That’s the same code he already has written a different way (ie wrong)
I might consider renaming the rotation variable as well… Cant see that it would conflict with the rotation member variable of Transform, but stranger things have happened.
Why is that wrong James? Why would that crash the game? Any reason or just being snippy again? Seriously, there is nothing wrong with the code I suggested Rasta and I am unsure what the issue is with your code.
Code seems fine. However you should set it to = 10 as mentioned and then on GetKeyUp set it back to 5
Right,
It depends on the effect.
If sprint is 10 always then James and BFGames are correct.
If you want to add 10 to the current speed each time you press, then what you were doing is correct. As for your error, I see none.
Cheers.

I dont think its actually the cause of the freeze. I was pointing out it might be wrong.
I wasnt being snippy (but now I am going to be). The solutions and comments I see you put in scripting are often wrong, or mis-informed.
As best I can tell, there are several logic flaws in the code, but I am making an assumption on how the character should run… ie, when holding sprint key, runs fast, when not, runs at normal speed. Ive never seen a game that continually increments the characters sprint speed.
Anyway, here is how I would write the code.
var runSpeed : float = 5.0;
var sprintSpeed : float = 10.0;
var rotationSpeed : float = 50.0;
function Update () {
var speed : float = Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : runSpeed;
var translation : float = Input.GetAxis ("Vertical") * speed * Time.deltaTime;
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed * Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
}
Hey thanks for all the help.
Sorry to be a pain in the butt but what does this line do?
var speed : float = Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : runSpeed;
That is identical to the following:
var speed : float;
if(Input.GetKey(KeyCode.LeftShit)
{
speed = sprintSpeed;
}
else
{
speed = runSpeed;
}