- Home /
Sprint cooldown(javascript)
so i just finished making my sprinting/energy bar class (javascript) this is it so far
#pragma strict
import UnityEngine.UI;
var energy : Image;
var playerEnergy : float;
var maxEnergy : float = 1;
private var chMotor : CharacterMotor;
private var controller : CharacterController;
function Start () {
chMotor = GetComponent(CharacterMotor);
controller = GetComponent(CharacterController);
playerEnergy = maxEnergy;
}
function Update () {
if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
{
chMotor.movement.maxForwardSpeed = 10;
chMotor.movement.maxSidewaysSpeed = 10;
playerEnergy -= Time.deltaTime / 3000;
}
else
{
chMotor.movement.maxForwardSpeed = 6;
chMotor.movement.maxSidewaysSpeed = 6;
}
if(playerEnergy > maxEnergy)
{
playerEnergy = maxEnergy;
}
if(playerEnergy < 0)
{
playerEnergy = 0;
}
energy.fillAmount = playerEnergy;
}
now im not sure how to make a sprint cooldown, im fairly new to coding overall and im really stuck with this. I tried using yield waitforseconds but im not sure how to use it, any ideas?
What do you mean by "sprint cool down"? Not sure what you are trying to achieve.
in most games after you sprint for a set amount of time youll go back to walking and you cant sprint for a while
Ooh, ok, so lets say you ran, your energy goes from 100 to zero, you want it so the player can't start sprinting again for a few seconds (Even if it regenerates)?
Answer by stevedarby02 · Feb 15, 2015 at 03:15 PM
Maybe something like this? Sure there is a better way, but this is a start
#pragma strict
import UnityEngine.UI;
var energy : Image;
var playerEnergy : float;
var maxEnergy : float = 1;
var canSprintTrigger : boolean = true;
private var chMotor : CharacterMotor;
private var controller : CharacterController;
function Start () {
chMotor = GetComponent(CharacterMotor);
controller = GetComponent(CharacterController);
playerEnergy = maxEnergy;
}
function Update () {
if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift) && canSprintTrigger == true)
{
chMotor.movement.maxForwardSpeed = 10;
chMotor.movement.maxSidewaysSpeed = 10;
playerEnergy -= Time.deltaTime / 3000;
canRun();
}
else
{
chMotor.movement.maxForwardSpeed = 6;
chMotor.movement.maxSidewaysSpeed = 6;
}
if(playerEnergy > maxEnergy)
{
playerEnergy = maxEnergy;
}
if(playerEnergy < 0)
{
playerEnergy = 0;
}
energy.fillAmount = playerEnergy;
}
function canRun()
{
yield WaitForSeconds(10); //you'll need to play around with how long someone can sprint for before it's set to false. Then again how long you wait until it's set to true again
canSprintTrigger = false;
yield WaitForSeconds(10);
canSprintTrigger = true;
}
i tried that in the beginning but i dont know were to go with that
Answer by TargonStudios · Feb 15, 2015 at 04:28 PM
Is it something like this you're looking for?:
#pragma strict
import UnityEngine.UI;
var energy : Image;
var playerEnergy : float;
var maxEnergy : float = 1;
var CanSprint = true;
private var chMotor : CharacterMotor;
private var controller : CharacterController;
function Start () {
chMotor = GetComponent(CharacterMotor);
controller = GetComponent(CharacterController);
playerEnergy = maxEnergy;
}
function SprintTimer(){
yield WaitForSeconds(1);
CanSprint = true;
}
function Update () {
if(Input.GetKeyUp(KeyCode.LeftShift)&&CanSprint==true)
{
CanSprint=false;
SprintTimer();
}
if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
{
if(CanSprint==true){
chMotor.movement.maxForwardSpeed = 10;
chMotor.movement.maxSidewaysSpeed = 10;
playerEnergy -= Time.deltaTime / 3000;
}else{
chMotor.movement.maxForwardSpeed = 6;
chMotor.movement.maxSidewaysSpeed = 6;
}
}
else
{
chMotor.movement.maxForwardSpeed = 6;
chMotor.movement.maxSidewaysSpeed = 6;
}
if(playerEnergy > maxEnergy)
{
playerEnergy = maxEnergy;
}
if(playerEnergy < 0)
{
playerEnergy = 0;
}
energy.fillAmount = playerEnergy;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613183301im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
counting seconds that character is idle. 1 Answer
How can I tell Unity to wait to do something until after and animation has finished playing? 0 Answers
Accessing A Variable Across Script In A Child Object 0 Answers
How can I change the maxForwardSpeed variable in the CharacterMotor script form a different script? 1 Answer
How do i make a character accelerate and play an animation when the click left-shift 1 Answer