Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Closest point on a Gamebject

Discussion in 'Scripting' started by sachithkp, Jul 13, 2021.

  1. sachithkp

    sachithkp

    Joined:
    Jun 7, 2021
    Posts:
    12
    I was trying to measure the distance from the Player position to the closest point on the gameobject.

    I was trying to achieve this without using colliders.

    How can we get all the points of a GameObject in an array. I believe comparing the Player distance will solve the problem.

    People, please shed some light.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Uhh, you would have to compare the distance between the faces and the point, because if you for example have a large, flat plane, you can't get the proper distance by just checking the points.
    You can't actually get this easily enough, not even with a collider.
    Share some more specifics, if your player is made of primitives or you're fine with that approximation, then it could be much easier and faster to do.
     
  3. sachithkp

    sachithkp

    Joined:
    Jun 7, 2021
    Posts:
    12
    I used this reference to calculate the distance between ray and Gameobject without using Collider.
    https://forum.unity.com/threads/edi...ect-using-gui-coordinate.485502/#post-7308457

    The requirement is to find the closest point of the player and the ray hit GameObject. In the image attached, you can see the player and a ray being passed from it. The ray hits the white cube below. I would like to find the closest point on the white cube from the player.

    upload_2021-7-13_13-50-11.png
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,644
    You could do some sort of BoxCast to find the closest hit point of the object, however using a box isn't going to be 100% accurate in finding the closest point, as it would be an orthographic projection