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:
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.
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.