Search Unity

Decreasing Player health when in a trigger zone C#

Discussion in 'Scripting' started by Theupside-downpenguin, Dec 11, 2014.

  1. Theupside-downpenguin

    Theupside-downpenguin

    Joined:
    Dec 11, 2014
    Posts:
    5
    My player has a script called PlayerHealth attached. Which works fine. I have also created a trigger zone, which I want placed under water to slowly decrease the players health. (also shown below). How do I get the player to slowly loose health whilst in the trigger zone?


    public class PlayerHealth : MonoBehaviour
    {
    public float startingHealth = 100f;
    public float currentHealth;

    void Awake()
    {
    currentHealth = startingHealth;
    }



    void Update()
    {
    if(currentHealth <= 0f)
    {
    Destroy(gameObject);
    }
    }
    }





    public class PlayerDrown : MonoBehaviour
    {
    void OnTriggerEnter (Collider drown)
    {
    if(drown.gameObject.tag == "Player")
    {

    Debug.Log("Player is drowning");

    }
    }
    }
     
  2. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
  3. Theupside-downpenguin

    Theupside-downpenguin

    Joined:
    Dec 11, 2014
    Posts:
    5
    How would I write it so that whilst staying in the trigger zone, to slowly remove health from the player? (I'm quite new to C#)
     
    Last edited: Dec 11, 2014
  4. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Code (CSharp):
    1. public class PlayerHealth : MonoBehaviour {
    2.     public float startingHealth = 100f;
    3.     public float currentHealth;
    4.  
    5.     void Awake() {
    6.         currentHealth = startingHealth;
    7.     }
    8.  
    9.     void Update() {
    10.         if (currentHealth <= 0f) {
    11.             Destroy(gameObject);
    12.         }
    13.     }
    14.  
    15.     public void remove(float amount) {
    16.         currentHealth -= amount;
    17.     }
    18. }
    Code (CSharp):
    1. public class PlayerDrown : MonoBehaviour {
    2.     void OnTriggerEnter(Collider drown) {
    3.         if (drown.gameObject.tag == "Player") {
    4.             PlayerHealth health = drown.GetComponent<PlayerHealth>();
    5.  
    6.             if (health != null) {
    7.                 health.remove(1);
    8.                 Debug.Log("Player is drowning");
    9.             }
    10.         }
    11.     }
    12. }
     
    EmmysGames likes this.
  5. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    The way I'd do that is as follows: create a public reference to the PlayerHealth script, and then inside OnTriggerStay, subtract health from the script. Note that OnTriggerStay is called on the physics timestep, which by default is every 0.02 seconds, so scale the damage to match.

    Code (csharp):
    1.  
    2. public PlayerHealth playerHealthScript;     // you can assign this in the Inspector before starting
    3. public float drowningDamage;
    4.  
    5. void OnTriggerStay(Collider drown)
    6. {
    7.    if(drown.gameObject.tag == "Player")
    8.    {
    9.       Debug.Log("Player is drowning");
    10.       playerHealthScript.currentHealth -= drowningDamage;
    11.    }
    12. }
    13.  
     
  6. Theupside-downpenguin

    Theupside-downpenguin

    Joined:
    Dec 11, 2014
    Posts:
    5
    Thanks for the help! Works great!
     
  7. CoreCoder

    CoreCoder

    Joined:
    Jun 11, 2015
    Posts:
    41
    You could use a Time.deltaTime for this function also to delay with a counter. That way you can determine if you want say for example every second the player will suffer 1 point of damage or what have you. I believe if you dont reference Time.deltaTime your dependent on framerate right?
    Something like :
    countdown -= Time.deltaTime;
    if(countdown <= 0)
    player looses 1 hitpoint.