Can you use OnCollisionExit with an OverlapSphere?
I need to simulate a gravitational field around a planet so I used an OverlapSphere
to have certain affects on objects once they enter its field. I need to know when it exits the OverlapSphere
so I can handle its velocity accordingly.
Answer by jgodfrey · May 04, 2016 at 02:42 AM
No, OverlapSphere won't interact with the OnCollisionExit method. I can see a few ways of doing what you need.
The first is to continue to use an overlap sphere to detect objects within range of the planet. You'd want to keep a "current" collection of in-range objects and a "previous" collection of in-range objects. Then, you'd just compare the objects in the current collection to those in the previous collection. Objects that are in the previous collection but not in the current collection have just moved out of range.
The other, likely simpler way to do this would be to replace your current OverlapSphere with a spherical collider. The collider would cover the same area as your OverlapSphere, but would allow you to use OnCollisionEnter/Exit methods...