Is there a way for Physics.CheckSphere to have OnEnter and OnExit?
I was wondering if there's a way to call functions like OnEnter and OnExit, which only get called once, using the Physics sphere. Right now I can only find a function CheckSphere() which returns true or false every frame its called. Mine is in the Update function right now, checking for the layer which the player collider is on.
so -- how can I call a method just ONCE when the player enters the sphere region, and call another method ONCE when the player exits the sphere region?
PS -- I'm not using rigidbodies or character controllers on these "trigger" regions. So don't waste your time telling me "just add a rigidbody". I don't want them. These sphere regions handle things on the game map, like for example, a player walks near a door and the door lights up signaling that the player can enter. Or for chatting with NPCs, entering a battle with a monster, etc a large variety of things. The player does have a character controller attached to him though.
Answer by $$anonymous$$ · Feb 20, 2017 at 05:26 AM
A potential solution is to just have a bool variable that holds the last value of the CheckSphere() and use that to call code on enter or exit. For example:
bool oldCheckSphere;
void FixedUpdate (){
// Add parameters here
bool checkSphere = Physics.CheckSphere(...);
if(oldCheckSphere == false && checkSphere == true){
// OnEnter
} else if (oldCheckSphere == true && checkSphere == false){
// OnExit
}
oldCheckSphere = checkSphere;
}
Just input whatever parameters into the Physics.CheckSphere(...) and I believe it should work.
Hope this helped!