Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Adding damage in collisons

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

  1. if (Input.GetKeyDown(KeyCode.E))
  2. {
  3. takeDamage(20);
  4. }

  5. void takeDamage(int damage)
  6. {
  7. currentHealth = currentHealth - damage;
  8. healthBar.setHealth(currentHealth);
  9. }

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;

    }

Sign In or Register to comment.