Not sure how you want your sprint too work. What kind of cool down do you want?
So you have it set up right now so that if they hold Shift down they will always be Sprinting right? So do you want them to just get tired eventually kind of like a stamina bar? Or do you want them to have a certain cool down timer every time after you use Sprint?
For something incredibly basic to get you started you could do something like this:
float sprintTimer = 1000;
void Update()
{
//quick example to include sprintTimer in your logic:
if ((charController.isGrounded) && (sprintTimer >= 2))
{
if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
speed = sprintSpeed;
}
}
}
void FixedUpdate()
{
//if player is Sprinting
if (speed == sprintSpeed)
{
if (sprintTimer >= 2)
{
sprintTimer += -2;
}
else
{
sprintSpeed = walkSpeed; //or crouch speed...
}
}
//if player is not Sprinting
else if (sprintTimer < 1000)
{
sprintTimer += 1;
}
}
In this example the player will drain their sprint “stamina” twice as fast as they will regenerate it, can use FixedUpdate for easy consistent timing here.
In Update the player will only be able to Sprint if they have enough sprintTimer “Stamina” left.
You can tinker with the variables to set it up just right but that’s probably one of the easiest ways to do this.
Coroutines are another good option for this as the previous poster mentioned, it depends what kind of system you want.
I’m not certain how to use them in JavaScript, but this soulds like a good place to use coroutines.
I would have a boolean switch called ‘CanSprint’ when the player starts sprinting a coroutine starts and yields for the ammount of time you want to be able to sprint and sets ‘CanSprint’ to false.
This can then be used to trigger another coroutine that yields for the ‘CoolDownTime’ and then sets ‘CanSprint’ to true.