- Home /
Why Isn't This Sprint Working?
Why isnt this working? I can see NO syntax problems or incorrectly typed variable names? It just doesnt work, theres no errors or anything, it just doesnt work?
var MoveSpeed = float;
var RunSpeed : float = 4.0;
var RunSpeed : float = 4.0;
var SprintSpeed : float = 8.0;
var JumpHeight : float = 6.0;
var Gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;
private var ButtonA : boolean = false;
private var ButtonD : boolean = false;
private var ButtonS : boolean = false;
function Start()
{
MoveSpeed = RunSpeed;
}
function Update()
{
var controller : CharacterController = GetComponent(CharacterController);
if(Input.GetButton("A"))
{
ButtonA = true;
}
else
{
ButtonA = false;
}
if(Input.GetButton("D"))
{
ButtonD = true;
}
else
{
ButtonD = false;
}
if(Input.GetButton("S"))
{
ButtonS = true;
}
else
{
ButtonS = false;
}
if (controller.isGrounded)
{
MoveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
MoveDirection = transform.TransformDirection(MoveDirection);
MoveDirection *= MoveSpeed;
if(Input.GetButtonDown("Sprint") && !ButtonA && !ButtonD && ButtonS)
{
MoveSpeed = RunSpeed * SprintSpeed;
}
if(Input.GetButtonUp("Sprint"))
{
MoveSpeed = RunSpeed;
}
if (Input.GetButton ("Jump"))
{
MoveDirection.y = JumpHeight;
}
}
MoveDirection.y -= Gravity * Time.deltaTime;
controller.Move(MoveDirection * Time.deltaTime);
}
function Sprint()
{
MoveSpeed = SprintSpeed;
}
function EndSprint()
{
MoveSpeed = RunSpeed;
}
Answer by Graham-Dunnett · May 25, 2013 at 09:21 AM
So, the error message will include the line number where the compiler found the error. If you locate this line, it should be "obvious" what's wrong. I looked through the code quickly and didn't see a problem. Let the community know the line number if you need more help.
LOL. Posted that answer and immediately saw line 61. Try there.
Line 52 has a similar problem. (Please don't post an answer when you mean to make a comment.)
Its just calling on the Sprint function, but for some reason it finds a problem?
I should also add that it's bad practice to end your if statements with semicolons, like this: if(Input.GetButtonUp("Sprint"));
Now, I'm not sure how Javascript handles it, but a lot of program$$anonymous$$g languages treat that as an empty if statement, so a code block like this:if(Input.GetButtonUp("Sprint")); { EndSprint(); }
Will call EndSprint() regardless of the condition, because the condition is on the empty statement (i.e. ;), not on the block beneath it.
Your answer
Follow this Question
Related Questions
Decreasing footstep length when key pressed 1 Answer
Increase speed of footsteps when sprinting? 2 Answers
Energy Pickup 1 Answer
Sprint animation play ONLY when speed = 13 1 Answer
How do you make a sprint script actually change your speed? 1 Answer