Hello,
I implemented a throttle a little bit differently and was wondering if my way has any downsides. One way I believe my implementation is better is that in my case, we only calculate the new time once ever attack instead of once every frame, which I think is much more efficient. Am I missing something that makes my code inferior to Sam’s or a reason I should use Sam’s so that it will work better with the game further down the course? I just have a float that starts at 0, then every time we attack, it sets this float to the current time + the interval to wait.
My Code:
[SerializeField] float attackInterval = 1f;
float nextAttackTime = 0;
private void AttackBehavior()
{
if (Time.time > nextAttackTime)
{
myAnimator.SetTrigger(“attack”);
nextAttackTime = Time.time + attackInterval;
}
}
Sam's:
[SerializeField] float timeBetweenAttacks = 1f;
float timeSinceLastAttack = 0;
private void Update()
{
timeSinceLastAttack += Time.deltaTime;
}
private void AttackBehaviour()
{
GetComponent().SetTrigger(“attack”);
if (timeSinceLastAttack > timeBetweenAttacks)
{
GetComponent().SetTrigger(“attack”);
timeSinceLastAttack = 0;
}
}