Search Unity

Repeating the taken damage within collider

Discussion in 'Scripting' started by rennster200, Jun 22, 2019.

  1. rennster200

    rennster200

    Joined:
    Jun 21, 2019
    Posts:
    65
    Hello, I made 2 scripts for taking damage from my zombie...Zombie is moving and I can kill him but...Whenever he gets in my collider I get damage and there is when it stops...The thing I want to make is to countinously take damage when Zombie is in my collider...
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyScript : MonoBehaviour
    6. {
    7.  
    8.     float damage = 5f;
    9.    
    10.    
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.    
    19.  
    20.  
    21.     void OnTriggerEnter(Collider other)
    22.     {
    23.        
    24.         other.gameObject.GetComponent<HealthScript>().TakeDamage(damage);
    25.     }
    26. }
    27.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HealthScript : MonoBehaviour
    6. {
    7.  
    8.  
    9.    
    10.     public float max_health = 100f;
    11.     public float cur_health = 0f;
    12.     public bool alive = true;
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.  
    19.         alive = true;
    20.         cur_health = max_health;
    21.        
    22.        
    23.     }
    24.  
    25.  
    26.     void DoDamage()
    27.     {
    28.  
    29.         TakeDamage(10f);
    30.  
    31.     }
    32.  
    33.     public void TakeDamage(float amount)
    34.     {
    35.            
    36.        
    37.         if (!alive)
    38.         {
    39.             return;
    40.         }
    41.  
    42.         if (cur_health <= 0)
    43.         {
    44.             cur_health = 0;
    45.             alive = false;
    46.             gameObject.SetActive(false);
    47.         }
    48.  
    49.  
    50.         cur_health -= amount;
    51.  
    52.  
    53.     }
    54.     // Update is called once per frame
    55.  
    56. }
    57.  
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    It is not good to write game logic in FixedUpdate. FixedUpdate is for physics calculations. Update and LateUpdate are for logic. FixedUpdate runs about 20 fps no matter what framerate is, it has own physics framerate setting. Also it may run multiple times per frame in case of lags. It may break your logic.
    The best strategy would be to save zombies you came in contact with to list in OnTriggerEnter and remove them from that list in OnTriggerExit. Then, in Update, you can iterave over the list of all zombies you're in contact with and apply all damage from them.