- Home /
If statment to check if player is within collider
I am creating a door that opens when the player enters the Collider box.
But how can i check to see if the player has left the collider box. So the door dosent close while the player is standing in the doorway, but automaticly closes when the player has moved away from the door.
I also need to make shure that if the player re-enters the collision box, while the door-close animation is playing. Then the door finishes the close animation but then immidetly plays the open animation.
Answer by duck · Mar 18, 2010 at 03:41 PM
Sounds like you're using the OnTriggerEnter function fine.
Now you just need to use the OnTriggerExit function to detect when the player leaves the collider.
If you really want to check whether the player is still currently within the collider, you could make use of the OnTriggerStay function, however from your description it sounds like the OnTriggerExit version is better suited.
In terms of the door completing its closing animation before re-opening, the implementation of that would be depending on what code you have already, however the general idea would probably involve having OnTriggerEnter and OnTriggerExit functions set a boolean flag, which allows the code to see whether the player is currently within the trigger area or not. Eg:
var playerInArea : boolean = false;
function OnTriggerEnter() { playerInArea = true; }
function OnTriggerExit() { playerInArea = false; }
You'd then just check whether the door should open based on both:
(playerInArea) and (door is completely closed)
hope this helps!
This is flawed.
If the player enters trigger, and exits before the animation is complete. The Bool will stay true, even with the player is out.
Need to check if the player "stayed" within trigger, this will also catch any "system hickups" where you could see someone enter a trigger but not triggering.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Collect some coins for Points 1 Answer
Physics.Raycast not checking layermask properly? 1 Answer
Continuously moving rigidbody 2 Answers
Colliders Not Doing Their Job 2 Answers