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. Dismiss Notice

Bug Getting the closet target to Player in a list of targets

Discussion in 'Scripting' started by DexterDev, Sep 30, 2023.

  1. DexterDev

    DexterDev

    Joined:
    Sep 6, 2020
    Posts:
    3
    Hi all,

    Ultimately I am attempting to create similar lock-on functionality to this - where the closest target amongst an array of targets is chosen, perhaps a higher priority given to a target that the Centre of the screen is closest to - but that's the end goal.

    See my reference below.

    https://youtube.com/clip/UgkxSzbsYgEXZv2sePDwPzKUR0CVVXan7hBb?si=2iNFtEdcbwzt8qiA

    Here is what my current code functionality is producing :



    My problem :

    It seems that the Vector3.Distance calculation is not correctly calculating which Target is closest. Oddly enough it seems to calculate a target that is further away as the nearest target rather than one that is directly closest.

    Now I'm assuming the problem could one of a few things here but I'm not sure :
    • That the visualization of the nearest target isn't being updated enough and therefore displaying the incorrect target.
    • That the math for distance calculation is not taking into account some key concept and therefore making the incorrect calculation.
    • That my current basic code just isn't correct.
    My script is below :

    Code (CSharp):
    1.    void FindVisibleTargets()
    2.    {
    3.  
    4.        visibleTargets.Clear();
    5.        nearestTarget = null;
    6.  
    7.        Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, viewRadius, targetMask);
    8.  
    9.        Transform nearestTargetInView = null;
    10.  
    11.        for (int i = 0; i < targetsInViewRadius.Length; i++)
    12.        {
    13.            Transform target = targetsInViewRadius[i].transform;
    14.            Vector3 dirToTarget = (target.position - transform.position).normalized;
    15.  
    16.            if (Vector3.Angle(transform.forward, dirToTarget) < viewAngle / 2)
    17.            {
    18.                float dstToTarget = Vector3.Distance(transform.forward, target.position);
    19.  
    20.                if (!Physics.Raycast(transform.position, dirToTarget, dstToTarget, obstacleMask))
    21.                {
    22.                    visibleTargets.Add(target);
    23.  
    24.                    if (nearestTargetInView == null)
    25.                    {
    26.                        nearestTargetInView = target;
    27.                    }
    28.  
    29.                    else if (nearestTargetInView != null)
    30.                    {
    31.                        float dstToPrevTarget = Vector3.Distance(transform.forward, nearestTargetInView.position);
    32.  
    33.                        Vector3 dir
    34.  
    35.                        if (dstToTarget > dstToPrevTarget)
    36.                        {
    37.                            nearestTargetInView = target;
    38.                        }
    39.                    }
    40.  
    41.                    nearestTarget = nearestTargetInView;
    42.                    Debug.Log("Nearest target is " + nearestTarget.gameObject.name);
    43.                }
    44.  
    45.            }
    46.        }
    47.    }
    The above is from my Player Controller script, there is another Editor script that is soley for displaying the visuals on the editor and is simply taking the Publicly declared nearestTarget variable and adding a green circle around it.

    Can anyone provide any guidance on what I am doing wrong here and perhpas also some future suggestions on how I'd manage prioritizing targets that are more central in the viewport over those whom are simply closer.

    Thanks
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    are you sure on
    Code (CSharp):
    1.  
    2.                        if (dstToTarget > dstToPrevTarget)
    3.                        {
    4.                            nearestTargetInView = target;
    5.                        }
    if you wanted nearest surely you want < not >
     
    DexterDev and CodeSmile like this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    I also see that as the issue here.
     
  4. DexterDev

    DexterDev

    Joined:
    Sep 6, 2020
    Posts:
    3
    @bugfinders @CodeSmile

    Wow, can't believe I overlooked that. Thanks guys