Sprinting doesn't work!

Why wont this code work? In the //Allows sprinting, i have the if statement to check whether the person has pressed the left shift, and if they have, change the actual speed of the player (5, stated at the beginning of //Movement of player) to the sprint speed (10). I don’t know what’s wrong D: Also if you know how to add a cooldown to the sprinting (say 3 seconds), please help me with that too :3 Da.

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(CharacterController))]

public class FirstPersonController : MonoBehaviour {

public float mouseSensitivity = 5.0f;
public float jumpSpeed = 20.0f;
public float upDownRange = 60.0f;
public float walkSpeed = 5.0f;
public float sprintSpeed = 10.0f;

float verticalVelocity = 0;
float verticalRotation = 0;
float actualSpeed;

CharacterController characterController;

// Use this for initialization
void Start () {

Screen.lockCursor = true; // Hides the cursor on playing
characterController = GetComponent (); //Retrieves CC

}

// Update is called once per frame
void Update () {

// ** Movement of Player **

actualSpeed = walkSpeed;
float forwardSpeed = Input.GetAxis (“Vertical”) * actualSpeed; //Movement speed of player - forward movement
float sideSpeed = Input.GetAxis (“Horizontal”) * actualSpeed / 2; //Movement speed of player - side movement

verticalVelocity += Physics.gravity.y * Time.deltaTime; //Applies acceleration p/s of Gravity when falling

// Restricts player from jumping twice in mid-air ir until player hits ground

if ( characterController.isGrounded Input.GetButtonDown (“Jump”)) {
verticalVelocity = jumpSpeed;
}

//Allows sprinting

if (characterController.isGrounded Input.GetKeyDown(KeyCode.LeftShift)) {
actualSpeed = sprintSpeed;
}
if (characterController.isGrounded Input.GetKeyUp(KeyCode.LeftShift)) {
actualSpeed = walkSpeed;
}

Vector3 speed = new Vector3 ( sideSpeed, verticalVelocity, forwardSpeed ); //Vector for movement of the player

speed = transform.rotation * speed; // ?

characterController.Move ( speed * Time.deltaTime ); //Applies speed of player p/frame

}

}

Please put your code in CODE tags so it’s easier to read…

Anyway, there are (at least) a few problems here:

  • You’re using GetKeyUp and GetKeyDown to determine if you’re character is sprinting or not. Those only fire once - in the frame where the key was pressed / released respectively. So, you’re only setting “actualSpeed” to the sprint speed for a single frame, then it gets reset to walkSpeed in the next Update call. Assuming you want to sprint while the Shift key is held down and walk when it’s released, you probably want to use GetKey instead. That’ll be true in every frame where the key is held down and false when it’s not held down.
  • By the time you get the “Sprint” code in Update, you’ve already used the “actualSpeed” variable to set the current speed (forwardSpeed / sideSpeed). So, by the time you decide if you’re sprinting or not, it doesn’t matter. So, you’ll need to rearrange some things to make this work out.

Jeff

Thanks a bunch :stuck_out_tongue: (you’re name reminds me of someone)

Does it work for you now?
I can’t make it work…
My only difference is that my characterController is called cc but thats it…