Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Get the position of an object nearest to a certain point?

Discussion in 'Scripting' started by KennethGames2020, May 27, 2020.

  1. KennethGames2020

    KennethGames2020

    Joined:
    Feb 26, 2020
    Posts:
    52
    Hi, I am working on a ship building game where players can build a ship out of blocks. In the ship editor there is the code:

    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    2.         RaycastHit hit;
    3.         if(Physics.Raycast(ray, out hit, 10f))
    4.         {
    5.  
    6.             if(hit.collider.tag == "Hull Mesh")
    7.             {
    8.                 Vector3 gnp = h.getNearestPos(hit.point);
    9.                 bp.setPos(gnp);
    10.             } if(hit.collider.tag == "Positioner Collider")
    11.             {
    12.                 ghostCube.transform.position = hit.collider.transform.position;
    13.  
    14.                 if (Input.GetMouseButtonDown(0))
    15.                 {
    16.                     ShipBuildable nb = sbd.shipBuildables[1].cloneBuildable();
    17.                     nb.pos = ghostCube.transform.position;
    18.                     h.sb.buildables.Add(nb);
    19.                     h.updateHullMesh();
    20.                 }
    21.  
    22.             }
    23.         }
    h is a variable representing the ship's hull, which holds the code for the ship's blueprint, which is a list of ship buildables. If a raycast from the main camera hits the ship's hull mesh it tries to retrieve the position of the buildable closest to the point of which the raycast hit on the hull.

    Can someone help me figure out how to code the getNearestPos()?

    So far all I came up with is this:

    Code (CSharp):
    1.     public Vector3 getNearestPos (Vector3 hitPos)
    2.     {
    3.         Vector3 invalidPos = new Vector3(0, 0, 0);
    4.  
    5.         Vector3 posRounded = new Vector3(Mathf.Round(hitPos.x), Mathf.Round(hitPos.y), Mathf.Round(hitPos.z));
    6.         int i = sb.buildables.FindIndex(j => j.pos == posRounded);
    7.         if(i != -1)
    8.         {
    9.             return sb.buildables[i].pos;
    10.         }
    11.  
    12.         return invalidPos;
    13.     }
    but it keeps seeming to return "invalidPos"... Please help.

    Thanks.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    FindIndex(j => j.pos == posRounded)
    is only going to find something at exactly the coordinates you specified, so unless the nearest object is right on top of you it will never work.

    To find the "best" option out of a list, you can do something like this:
    Code (CSharp):
    1. int bestIndex = -1;
    2. float bestDistance = float.MaxValue;
    3.  
    4. for (int i = 0; i < myList.Count; ++i)
    5. {
    6.     // Square magnitude is used instead of magnitude because it is
    7.     // cheaper to calculate and won't change the relative ranking
    8.     float distance = (myList[i].position - targetPosition).sqrMagnitude;
    9.     if (distance < bestDistance)
    10.     {
    11.         bestDistance = distance;
    12.         bestIndex = i;
    13.     }
    14. }
    15.  
    16. // The closest thing in myList is myList[bestIndex]
     
    KennethGames2020 likes this.
  3. KennethGames2020

    KennethGames2020

    Joined:
    Feb 26, 2020
    Posts:
    52
    Thank you.