- Home /
health depleat with colliders
Im having a slight problem with some code and could do with some help. When the player is walking around their health depleats and when they enter these shelters their health stays the same. When they go back into the world their health should start to depleat again.
I have sent up colliders on the "shelters" and one over the entire level. When the user walks into the shelter the collider recognises this and stops depleating the health. When they walk out of the shelter though the health stays the same.
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.tag == "shelter") { print("You have entered a shelter. health will cease to depleat now"); //check if player has collided with a shelter - health will not depleat. healthDepleat = false; //health = health; } //else if(hit.gameObject.tag == "level") { print("Your on the surface, health will start to depleat"); //check if player is not in a shelter - health will depleat healthDepleat = true; //health--; } }
This is the code i have currently, any ideas on where i am going wrong. I need the code to recognise when the player has left that shelter of gone out of the collider. I have done it in Java
thanks in advanced
Answer by Noah-1 · Oct 22, 2012 at 09:17 PM
To detect when your player walks out of the shelter, you can use OntriggerExit, with that, you can keep track of the player entering and getting out of the terrain, it would be something like this:
var hit: Collider;
function OnTriggerExit (hit : Collider)
{
//Decrease Health
}
Hope it helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082741im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Head shoot and body shoot not working with realistic fps characterDamage 0 Answers
Trigger enter in single direction ??? 3 Answers
Tagging a collider? 2 Answers
Collider waiting for two differently tagged game objects to enter before doing something 1 Answer
Two Objects in the same position? 0 Answers