- Home /
Find closest point of a sprite to GameObject 2D
Hello all, i am currently making a 2d game in unity where the characters traverse small worlds. I have a wonderful gravity code for it as well, but it only works for circular planets. I want to make planets shaped like plus signs and triangles and stuff like that. To do that i need to find the closest point of all planet's within a layer so i can apply forces to the character. All my attempts to achieve this goal have not worked. I want to know a way to accomplish Collider.findCloseestPointInBounds But in 2d. Collider2d does not have a closest point in bounds function. I tried to make one by shooting raycasts in every direction and finding the closest hit point, but i fear that would slow down the game when 20 objects have that code. Any suggestions?
Answer by iabulko · Sep 23, 2016 at 05:39 AM
Try shooting a ray in direction of center of planet, it will hit a face that is the ground. Then just jest that face's normal vector. And that's it ;>
This is not the solution. This is the code i have in place now. I have every object casting lines to every planet and ginding the closest, which only works if the planets are round.
Your answer
![](https://koobas.hobune.stream/wayback/20220612093323im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast not working on object without gravity 0 Answers
Trigger only one collider? 1 Answer
Change polygon collider depending on the animation 0 Answers
Why is OnDrag triggering but not OnMouseDown? 0 Answers
Change particle bounce relative to the speed of the GameObject colliding with them? 1 Answer