I have the following code:
using UnityEngine;
using System.Collections;
public class CooldownTest : MonoBehaviour {
public float startTimer = 0.0f;
public float stopTimer = 3.0f;
public bool onCooldown = false;
CharacterController characterController;
// Use this for initialization
void Start () {
characterController = GetComponent<CharacterController>();
} //Start function end
// Update is called once per frame
void Update () {
Sprinting();
sprintLimit();
} //Update end
private void Sprinting()
{
if (characterController.isGrounded Input.GetKey(KeyCode.LeftShift)
Input.GetKey(KeyCode.W) onCooldown == false)
{
startTimer = Time.time;
onCooldown = false;
stopTimer = 3f;
FirstPersonController.forwardSpeed = Input.GetAxis("Vertical") * FirstPersonController.movementSpeed * 2;
}
} //Sprinting end
private void sprintLimit()
{
if (startTimer >= 3)
{
stopTimer -= Time.time;
onCooldown = true;
startTimer = 0;
FirstPersonController.forwardSpeed = Input.GetAxis("Vertical") * FirstPersonController.movementSpeed;
}
if (stopTimer <= 0)
{
onCooldown = false;
stopTimer = 3;
Sprinting();
}
} //sprintLimit end
} //Script end
I’m using it to allow the player to sprint (with cooldowns enabled). I must use Time.time for the timer, but Time.time is calculating the time since the game started, not since the variable started. When playing, if I wait 5 seconds, startTimer will be at 5 seconds instead of starting at 0 when the player starts to sprint. How can I get the time that the variable was created? I’ve seen people using something like Time.time - theTimeVar
, but how on earth do I get the time variable?
Thanks 
Time.time is a float that contains the amount of time since the game started.
You can do something like:
var endCooldownTime=0.0f;
var coolTime=3.0f;
OnUpdate()
{
if(Sprinting Time.time>EndCooldownTime)
EndCooldownTime=Time.time+coolTime
}
-or- if you want to cool down to be able to be paused when the game gets paused
var coolDown=0;
var coolTime=3.0f;
OnUpdate()
{
if(!paused)
coolDown-=Time.deltaTime;
if (Sprinting coolDown<=0)
coolDown=coolTime;
}
And that will do what? Sorry, i’m kinda new.
I mean, say that Time.time is = 3.
If they’re sprinting and 3 > 0 then that 0 = 3 + 3 = 6. What is that 6? What does it represent?
If you’re setting startTimer to Time.time then you should be subtracting Time.deltaTime every frame and checking for when it’s <= 0 which means the cooldown has completed.
@Paragon - where is OnUpdate coming from?
The first snippet:
At time=0, endCoolTime=0
At time=3 they sprint… 3>0 then endCoolTime = 3+3=6. The sprint is On cool down until Time=6 (three seconds after they sprinted)
At time=5 they sprint 5<6 the sprint is still on cooldown, can’t sprint.
At time=7 they sprint 7>6 then endCoolTime = 7+3=10. They can sprint and sprint will be on cool down until Time=10
The second snippet:
At time=0 the coolDown (remaining cooldown timespan) is zero… they can sprint.
At time =3 they sprint, coolDown is less than zero (-3) so they sprint and coolDown is set to 3
At time = 5 they sprint, coolDown is now 1: (3 initial- 2seconds since) they can not sprint.
at Time = 7 they sprint, cooldown is now -1: (3 initial - 5 seconds since) so they sprint cooldown set to 3
at Time = 8 they pause (coolDown stops decrimenting)
at time =18 they unpause (cooldown starts decrimenting)
at Time =19 the sprint( cooldown is now 1) they can not sprint
… etc.
I really didn’t understand any of that
And I’m not sure how to integrate it into my code either. All I need is to get the starting time of startTimer when it’s called inside the if, that way I can say: If 3 seconds have passed since startTimer was set to Time.time (or whatever), then apply the cooldown function which will stop the sprinting, and after 3 seconds allow the player to continue.
@KelsoMRK: OnUpdate was meant to be the update function. Sorry, I was just quickly jotting down some psuedo code.
Cooldown Script
using UnityEngine;
using System.Collections;
//Define a delegate for the callbacks and the trigger
public delegate void CallbackHandler(object sender, object args); //You can just use the c# delegate that has the same sig... but this is it's sig for ref.
public delegate boolean CooldownTrigger(object sender, object args);
public class CooldownTest : MonoBehaviour {
//Inspectable Attributes (Is there a specific name?)
public float CooldownLength = 3.0f;
public bool Paused=false;
public float CooldownModifier=1.0f;
//Internal state;
float CooldownRemaining = 0.0f;
//Script to Script Properties
public bool IsCoolingDown { get{ return CooldownRemaining>0; } }
public float PercentReady { get{return (CooldownLength-CooldownRemaining)/CooldownLength*100;}}
public object TriggerArguments{get;set;}
//Events that can be subscribed to
public event CallbackHandler OnCooldownStarting; //What to do when the cooldown is initially set
public event CallbackHandler OnCooldownEnding; //What to do when the cooldown finished
public event CallbackHandler OnCooldownFailing; //What to do when the the cooldown attempts to set but is currently on cooldown
//Trigger functions (Return true to start cooldown)
public CooldownTrigger Trigger;
//Safe Event Dispatch funcs
CooldownStarting(object args){ if (OnCooldownStarting!=null) OnCooldownStarting(this, args); }
CooldownEnding(Object args){ if (OnCooldownEnding!=null) OnCooldownEnding(this, args);}
CooldownFailing(Object args){ if (OnCooldownFailing!=null) OnCooldownFailing(this, args);}
void Start () {
}
void Update ()
{
if (Paused) return;
if (CooldownRemaining>0)
{
CooldownRemaining-=Time.deltaTime*CooldownModifier;
if (CooldownRemaining<=0) CooldownEnding(Args);
}
if (Trigger==null) return;
if(Trigger(this,Args))
{
if (IsCoolingDown) CooldownFailing(null);
else
{
CooldownRemaining = CooldownLength;
CooldownStarting(null);
}
}
}
} //Script end
Script for object Using cooldown
void Start () {
characterController = GetComponent<CharacterController>();
sprintCooldown = GetComponent<CooldownScript>();
sprintCooldown.Trigger = new CooldownTrigger(SprintTrigger);
sprintCooldown.OnCooldownStarting+=new CooldownEventHandler(sprint_onStarting);
sprintCooldown.OnCooldownEnding += new CooldownEventHandler(sprint_onEnding);
sprintCooldown.OnCooldownFailing+= new CooldownEventHandler(sprint_onFailure);
}
private void SprintTrigger(object cd)
{
return (characterController.isGrounded Input.GetKey(KeyCode.LeftShift) Input.GetKey(KeyCode.W));
}
private void sprint_onStarting(object sender, object args)
{
Debug.Log("Started Sprinting");
FirstPersonController.forwardSpeed = Input.GetAxis("Vertical") * FirstPersonController.movementSpeed * 2;
}
private void sprint_onEnding(object sender, object args)
{
Debug.Log("Done Sprinting");
/*Code to stop sprinting ?*/
}
private void sprint_onFailure(object sender, object args)
{
Debug.Log("Attempting sprinting, but sprinting was on cool down.");
}