- Home /
How to add a cooldown sort of thing.
I need to add a cooldown for my script to make sure that I don't instantly drop the object as soon as I pick it up. I think I'd be something like float TimeDown = 1S;.
This is the important part of my script:
void Update() {
// If another item isn't picked up at the moment.
if(ItemPickedUp == false) {
// If you're looking at the item.
if (Physics.Raycast(CameraMain.transform.position, CameraMain.transform.forward, RayCastLength, RayMask.value)) {
// If you press the E key.
if (Input.GetKeyDown("e")) {
// The CarryableObject becomes a child of the Character.
CarryableObject.transform.parent = Character.transform;
// The position relative to the character.
CarryableObject.transform.position = new Vector3(Character.transform.position.x + 1, Character.transform.position.y + 1, Character.transform.position.z + 1);
// The rotation relative to the character.
CarryableObject.transform.rotation = Character.transform.rotation;
// The gravity.
CarryableObject.GetComponent<Rigidbody>().useGravity = false;
// The boolean that activates the boolean that is needed for the dropping code.
ItemPickedUp = true;
// The child collider if-statements.
if(Child1 != null){
Child1.GetComponent<Collider>().enabled = false;
}
if(Child2 != null){
Child2.GetComponent<Collider>().enabled = false;
}
if(Child3 != null){
Child3.GetComponent<Collider>().enabled = false;
}
if(Child4 != null){
Child4.GetComponent<Collider>().enabled = false;
}
}
}
}
// If you're holding an item.
if(ItemPickedUp == true){
// If you press the E key.
if(Input.GetKeyDown("e")){
// The CarryableObject becomes a child of the Utility Props again.
CarryableObject.transform.parent = UtilityProps.transform;
// The drop position.
CarryableObject.transform.position = new Vector3(Character.transform.position.x, Character.transform.position.y, Character.transform.position.z + OffsetDropPosition);
// The rotation is no longer relative to the Character.
CarryableObject.transform.rotation = Character.transform.rotation;
// The gravity gets reenabled.
CarryableObject.GetComponent<Rigidbody>().useGravity = true;
// The pick up boolean.
ItemPickedUp = false;
// The child collider if-statements.
if(Child1 != null){
Child1.GetComponent<Collider>().enabled = true;
}
if(Child2 != null){
Child2.GetComponent<Collider>().enabled = true;
}
if(Child3 != null){
Child3.GetComponent<Collider>().enabled = true;
}
if(Child4 != null){
Child4.GetComponent<Collider>().enabled = true;
}
}
}
}
cooldowns are pretty simple, you can go up or down with them or whatever you want. Also there is a few ways to do them, I will provide you with 2 examples.
EXA$$anonymous$$PLE 1:
public float Cooldown;
private float cooldown;
public bool isOnCooldown;
public void DoStuff()
{
if (!isOnCooldown)
{
//stuff happpens
isOnCooldown = true;
}
}
void Update()
{
if (isOnCooldown)
{
cooldown += Time.deltaTime;
if (cooldown > Cooldown)
{
isOnCooldown = false'
cooldown = 0;
}
}
}
EXA$$anonymous$$PLE 2:
public bool isOnCooldown;
public float Cooldown;
public void DoStuff()
{
if (!isOnCooldown)
{
//stuff happens
isOnCooldown = true;
Invoke("ResetCooldown", Cooldown);
}
}
private void ResetCooldown()
{
isOnCooldown = false;
}
Answer by TheShadyColombian · May 13, 2017 at 03:58 PM
One robust way of doing this is creating a float timer that you add deltaTime to during update, sort of like this. timerActive is a bool that is true when you want the timer to go.
void Update () {
if (timerActive) {
timer += Time.deltaTime;
}
}
and then check and reset the timer like this
if (timer >= delay) {
//Do stuff
timerActive = false;
timer = 0;
}
Your answer
Follow this Question
Related Questions
Timer counting UP instead of DOWN 1 Answer
How to work a real life timer? 2 Answers
How to make a Time Based Score? 1 Answer
Multiple Cars not working 1 Answer
Game Timer help needed 2 Answers