I want to add a two-second delay in the time I get hurt, an invincibility period. So, when I touch the enemy, I take one damage and have two seconds to be able to take more damage. Does anyone know how to do this? I thought I could use a coroutine to have it wait two seconds before activating again, but that didn’t work.
You just need a cooldown timer except that instead of preventing you from casting the spell, it inhibits damage.
Cooldown timers, gun bullet intervals, shot spacing, rate of fire:
GunHeat (gunheat) spawning shooting rate of fire:
To implement a delay in time between when the player gets damaged in Unity, you can use a Coroutine. A Coroutine is a function that can be paused for a specified amount of time before it continues to execute.
Here is an example of how you can use a Coroutine to implement a delay in time between when the player gets damaged:
using System.Collections;
using UnityEngine;
public class Player : MonoBehaviour
{
// The amount of time to delay before the player can be damaged again
public float damageDelay = 1.0f;
// A flag to track whether the player is currently invulnerable
private bool invulnerable = false;
// This function is called when the player gets damaged
public void OnDamage()
{
// If the player is already invulnerable, return immediately
if (invulnerable) return;
// Set the invulnerable flag to true
invulnerable = true;
// Start the damage delay Coroutine
StartCoroutine(DamageDelay());
}
// This Coroutine will wait for the specified amount of time before setting the invulnerable flag to false
private IEnumerator DamageDelay()
{
// Wait for the specified amount of time
yield return new WaitForSeconds(damageDelay);
// Set the invulnerable flag to false
invulnerable = false;
}
}
In this example, the OnDamage function is called whenever the player gets damaged. It sets the invulnerable flag to true, which prevents the player from getting damaged again until the DamageDelay Coroutine has finished executing. The DamageDelay Coroutine waits for the specified amount of time (in this case, damageDelay seconds) before setting the invulnerable flag back to false, allowing the player to be damaged again.
You can call the OnDamage function whenever the player gets damaged, for example in an enemy’s OnCollisionEnter function when the player collides with the enemy.
Thanks a bunch, Ill implement this
For things like that it doesn’t really make much sense to use coroutines. You still need the state variables in your class and coroutines would just add garbage and overhead. Just do
public float damageDelay = 1.0f;
float m_DamageCooldown = 0;
void Update()
{
if(m_DamageCooldown > 0)
m_DamageCooldown -= Time.deltaTime;
}
void OnDamage()
{
if (m_DamageCooldown > 0)
return;
m_DamageCooldown = damageDelay;
// apply damage here
}
In general you want to avoid starting and stopping coroutines all the time.
Is possible to overflow the coroutine if you make a small error. Such as starting corouta 1000s of times. With the update you only got the one instance per instance of script. So you couldn’t overflow it. You just reset the one damage clock to damage time if somebody else attacked during countdown
while courtesan she will run simultaneous for each projectile hit basically makes a new timer instance and so if I make a super weapon who apply 1000s of damage courotines without consideration then I imagine it would take a performance hit vs having one timer be effected.