Home > Back-end >  Unity Take damage while colliding
Unity Take damage while colliding

Time:10-12

Hey i am making a zombie game and i want everytime the zombie colides with my player it should deal damage every 2 seconds as long as they are colliding it should continue. while not colliding it should stop. what i did is this code bellow and the problem with it is that when i collide with the zombie it does damage once and stops i need to collide with it again for it to deal damage can anyone help so the zombie deals damage as long as they are colliding and the damage should be dealt every 2 seconds, thanks for the help :)

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

public class PlayerHealth : MonoBehaviour
{

    
    public float Health = 100f;
    public bool gameOver;
    private Animator playerAnim;
    float timeColliding;
    // Start is called before the first frame update
    private void Start()
    {
        playerAnim = GetComponent<Animator>();
    }
    private void Update()
    {
        if (Health <= 0)
        {
            playerAnim.SetBool("PlayerDeath", true);
            gameOver = true;
        }

    }
    void OnCollisionEnter(Collision collision)
    {
        
        if (collision.gameObject.tag == "Enemy")
        {
            
            Debug.Log("Enemy started colliding with player.");


            this.Health -= 10;

        }
      
    }
}

CodePudding user response:

I'm not good at unity, but i think something like this will work. Place all code inside while loop in my code to function in your code called to detect collision. And place isEven outside that function. You can test my code in c# compiler. For example if you replace //reduce health here with Console.WriteLine("reduced"); this code will print reduced every 2 second

public class Program
{
    public static void Main()
    {
        bool isEven = false;
        while(true){
            var timeSpan = DateTime.Now;
            if(timeSpan.Second %2 == 0){
                if(isEven == false){
                    //reduce health here
                }
                isEven = true;
            }
            else{
                isEven = false;
            }
        }
    }
}

In your code it will look like this:

public class PlayerHealth : MonoBehaviour
{

    bool isEven = false;
    public float Health = 100f;
    public bool gameOver;
    private Animator playerAnim;
    float timeColliding;
    // Start is called before the first frame update
    private void Start()
    {
        playerAnim = GetComponent<Animator>();
    }
    private void Update()
    {
        if (Health <= 0)
        {
            playerAnim.SetBool("PlayerDeath", true);
            gameOver = true;
        }

    }
    void OnCollisionEnter(Collision collision)
    {
        
        if (collision.gameObject.tag == "Enemy")
        {
            
            Debug.Log("Enemy started colliding with player.");
            var timeSpan = DateTime.Now;
            if(timeSpan.Second %2 == 0){
                if(isEven == false){
                    //reduce health here
                    this.Health -= 10;
                }
                isEven = true;
            }
            else{
                isEven = false;
            }


        }
      
    }
}
  • Related