Imagine a Sphere Collider that is placed on your player. Your player is placed in the very center of that sphere.
Let’s say it’s 10 feet in diameter.
The problem.
How to detect when a game object enters there sphere?
How to get the distance from the player?
How to get whether the game object is above, below, front or back?
I’ve tried OverlapSphere, InverseTransformPoint and Vector3.Dot.
Which seem like they might work but I don’t know how to find out if above, below, etc.
Any help is really appreciated.
Thanks!
OverlapSphere is a good option but if you want to do it continuously you might as well create a trigger SphereCollider around the object. Add an OnTriggerEnter method to get the objects entering the sphere, use Vector3.Distance(…) method to get the distance (contrary to popular belief, you should not worry about the performance cost of this method on a pc). For the direction it’s a bit more complicated…
You can use dot as you said, if you get the direction towards the other object then compare that direction to the objects transform.up, down / forward, back / left right, you should get what you need. This may be a little complicated though. Personally i would just get the InversTransformDirection, normalize it and check the vector values. For example if the direction.x is negative then the object is on the left.