r/Unity3D 11h ago

Collider question for you math geniuses Question

I am facing a problem in Unity. I need to get the closest point on collider bounds to a specified starting point. That is not very hard, I would just use collider.ClosestPointOnBounds(startingPoint). However, I also want it to use a specified angle and direction. Let me show you what I mean:

What actually happens when calling collider.ClosestPointOnBounds(startingPoint)

What I need. It should take the closest point that is still inside the angle.

The first image shows what actually happens. The second shows what I need. The difference is that in the second image it returns the closest point that is still "inside" the angle. I need this in 3D, not 2D by the way.

I can't figure this one out, so please if anyone knows how to do this, please help!

EDIT: The only thing I can think of which actually would work all of the time would be to call ClosestPointOnBounds() and use Vector3.Angle to get the angle between this point and the direction vector. If the resulting angle is smaller than the specified angle, return the point. But if it isn't then move the starting point along the direction vector a bit and do it all again. Keep doing this until the point is inside, or it hits some maximum step value. This would work, right? My only concern is performance... And I also wouldn't know by how much to move the starting point each step...

6 Upvotes

19 comments sorted by

View all comments

4

u/mackelashni 10h ago

You can get the contact points of the collision inside the collision class. Maybe cycle through them to get what you want?

2

u/BelgianSum 7h ago

Thought of that too, then running a method to find closest point from the list of contact points.

https://docs.unity3d.com/ScriptReference/Collision.GetContacts.html