Time.deltaTime + or -

Hello, I’m a beginner and I have a question. I have these 2 examples. The first taken from
__ C# Jump CoolDown __ and the second was given by my teacher in class:

First example:

float actionCooldown = 5.0f;
float timeSinceAction = 0.0f;

void Update()
{
timeSinceAction += Time.deltaTime;
}

void PlayerPressesActionButton()
{
if (timeSinceAction > actionCooldown)
{
timeSinceAction = 0;
PerformAction()
}
}

Second Example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class disparadorAuto : MonoBehaviour
{
public GameObject prefab;
public Transform posRef;
public float CoolDown;
private float coolDown;

// Start is called before the first frame update
void Start()
{
coolDown = -1;
}
// Update is called once per frame
void Update()
{
coolDown -= Time.deltaTime;
if(Input.GetButton(“Fire1”) && coolDown<=0f)
{
Instantiate(prefab, posRef.transform.position, posRef.transform.rotation);
}
coolDown = CoolDown;
}
}

My question is, why in the first example Time.deltaTime is added, and why in the second is substracted.

I hope for your help.
Thank you very much!!

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

Because one is a timer going up (time since action) and one is a timer going down (cooldown timer).

It’s just to do with how they designed what the program does.

1 Like

When programming, there are often many different ways to accomplish the same thing.
This is one of those things - an action that can only be performed after a certain amount of time has passed. Both examples are just calculating & checking the time using different methods.

I will say though that the second example does have a problem, not with its logic, but with it’s variable names.
There is CoolDown, which is how much time needs to pass, and coolDown, which is how much time is remaining. Having the same name for two different purposes is just going be confusing in the long run.

coolDown could maybe be renamed to remainingCoolDown, or something similar.

1 Like

Thank you, this is very useful !

Thank you ! I will take your advice into account!