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

Find Closest Target, they all find same object

Discussion in 'Scripting' started by panim0_unity, Jan 16, 2020.

  1. panim0_unity

    panim0_unity

    Joined:
    Nov 13, 2018
    Posts:
    47
    I have this script attached to my friendlyUnits, one timeToAttack is true they move towards to same enemyUnit and believe me one enemyUnit is closer than the other one to my second friendlyunit.

    Code (CSharp):
    1.  GameObject closest;
    2.     PlayerControls controls;
    3.     float speed = 7;
    4.     bool foundControls;
    5.     void Start()
    6.     {
    7.         foundControls = false;
    8.         FindClosestEnemy();
    9.         controls = GameObject.FindGameObjectWithTag("GamePlay").GetComponent<PlayerControls>();
    10.     }
    11.  
    12.    
    13.     void Update()
    14.     {
    15.        
    16.         if (controls.timeToAttack == true)
    17.         {
    18.             float step = speed * Time.deltaTime;
    19.            gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, closest.transform.position, step);
    20.         }
    21.     }
    22.  
    23.     #region findClosest
    24.     GameObject FindClosestEnemy()
    25.     {
    26.         GameObject[] gos;
    27.         gos = GameObject.FindGameObjectsWithTag("Player");
    28.  
    29.         float distance = Mathf.Infinity;
    30.         Vector3 position = transform.position;
    31.         foreach (GameObject go in gos)
    32.         {
    33.             Vector3 diff = go.transform.position - position;
    34.             float curDistance = diff.sqrMagnitude;
    35.             if (curDistance < distance)
    36.             {
    37.                 closest = go;
    38.                 distance = curDistance;
    39.             }
    40.         }
    41.  
    42.         if(closest!=null)
    43.         Debug.Log(closest.name);
    44.  
    45.         return closest;
    46.     }
    47.     #endregion
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Are you sure all of the objects have the correct tag on them? (Try Debug.Log("Found "+gos.Length+" objects"); after line 28 to make sure it's finding the expected number of objects)
     
  3. panim0_unity

    panim0_unity

    Joined:
    Nov 13, 2018
    Posts:
    47
    Yes it finds all 3 units but still 2 of them move towards to same one "(fri) = = (enm)" they are placed like this
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    One thing I notice is that you call FindClosestEnemy during Start, so whichever one is closest at the instant the object is created would be the one it finds. Do these objects, or the targets, ever move? If so, you'll probably want to call FindClosestEnemy when timeToAttack is first set to true.
     
  5. panim0_unity

    panim0_unity

    Joined:
    Nov 13, 2018
    Posts:
    47
    yes it worked well thanks, now trying to figure out why this is only workin on unity play but not on build for multiplayer