[SOLVED]C#: When hit by collision player only taking damage once.

Hello everyone,

ok so after messing with my game for awhile I decided to redo the way my player receives damage. The only problem now is that when the enemies hit my player he only gets damaged once and not continuously. If possible can someone help me to make it so that when my player is hit he takes damage continuously?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerHealth : MonoBehaviour {

    public int health;
    public Slider healthbar;
    public EnemyDamageHandler enemy;

    void Start(){
    }

    void Update(){
        healthbar.value = health;
    }

    void OnTriggerEnter2D(){
       
            health -= enemy.zombieDamage;

            Debug.Log ("Player Hit!");
       
           
       




        if(health <= 0){
            Die();
        }
    }

    void Die(){
        Destroy(gameObject);
    }
}

Thanks in advance

Just change void OnTrigger__Enter__2D to void OnTrigger__Stay__2D.

2 Likes

Awesome that works but also causes insta death :confused: Is there a wait to delay the script for a few seconds before taking damage again? I know I cant use Time.deltatime because its an Int.

Simple timer…

    float curTime = 0;
    float nextDamage = 1;


    void OnTriggerStay2D()
    {
        if (curTime <= 0) {
            Debug.Log ("Damage");

            curTime = nextDamage;
        } else {
     
            curTime -= Time.deltaTime;
        }
    }
2 Likes

Great that worked like a charm. Will keep that in mind next time thank you.