
It looks like you're new here. If you want to get involved, click one of these buttons!
I am making my first platformer and am stuck on the health system. I don't know how to take damage on collisions with enemies. Here is my simple code
if (Input.GetKeyDown(KeyCode.E))
{
takeDamage(20);
}
void takeDamage(int damage)
{
currentHealth = currentHealth - damage;
healthBar.setHealth(currentHealth);
}
This is all in the update function.
All I need to do is replace the press of the E button with a collision with any object with the tag "enemy" also what would be very helpful is if there was a 1 second delay before you could take damage again, so you know, it doesn't drain your health in a split second
Thanks a lot in advance :)
Answers
bool vulnerable = true;
public int invunerbleTime;
void On CollisionEnter(Collision c)
{
if (c.gameObject.tag == "enemy " && vulnerable = true)
{
takeDamage(20);
startcoroutine(countdown(invunerbleTime));
}
}
IEnumerator countdown(int i)
{
vulnerable = false;
yield return new WaitForSeconds(i);
vulnerable = true;
}