- Home /
Sprint Script??
I need Help wrighting a script to make the player sprint ive added this :
if (Input.GetKeyDown(KeyCode.LeftShift));
(maxForwardSpeed == 15.0);
Into the Character motor (script) on unity premade scripts and it wont work :(
Answer by tw1st3d · Jul 30, 2013 at 06:46 AM
function Update()
{
if(Input.GetKeyDown(KeyCode.LeftShift));
maxForwardSpeed = 15.0;
else
maxForwarSpeed = 10.0;
}
try putting in an Update() function in the script. I've never really dealt with modifying a script that wasn't mine, so I don't know if there's a smarter way to change it other than adding it into the file itself, another member could answer that. Anyway, you also forgot to add in an else statement to set it back to normal. So, here you go.
Old thread I know but this helped. Just thought I'd say however, that $$anonymous$$es didn't work with "Get$$anonymous$$eyDown", had to just use "Get$$anonymous$$ey" ins$$anonymous$$d (just for anyone else who comes across this in the future!).
Answer by save · Jul 30, 2013 at 06:47 AM
You should not end your if-statement with semicolon, what probably has confused you is that you don't need braces for an if-statement if you only have one operation depending on it.
// Example without braces
if (Input.GetKeyDown(KeyCode.LeftShift))
maxForwardSpeed = 15.0;
// Example with braces
if (Input.GetKeyDown(KeyCode.LeftShift))
{
maxForwardSpeed = 15.0;
}
Another thing is that (maxForwardSpeed == 15.0) is a boolean operation, maxForwardSpeed = 15.0 will assign 15.0 to the variable.
For additional help on adding a run function to your character controller please see How to Make the FPS Character RUN and CROUCH.
Actually, if statements only need brackets if they're calculating one operation in its scope, example
if(condition)
if(condition2)
if(condition3)
{
operation1;
operation2;
}
Otherwise, good answer.
Thanks @tw1st3d for making my slurred expression more evident. :-)