Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Michealunity · Nov 23, 2015 at 08:18 PM · scripting problemenemydistanceissueteam

Half-issue with find enemy script

HI guys this is the script that i write, i know that is not the best should be work but don't ,or better work but to half script more or less and i don't understand why.

The script :

 var teamMan : GameObject;
 
 var PlayerList1 : List.<GameObject> = new List.<GameObject>();
 var PlayerList2 : List.<GameObject> = new List.<GameObject>();
 var PlayerList3 : List.<GameObject> = new List.<GameObject>();
 var PlayerListnoally : List.<GameObject> = new List.<GameObject>();
 
 var limit : GameObject;
 var distlimit : float;
 
 var dist_team1_1 : float;
 var dist_team1_2 : float;
 var dist_team1_3 : float;
 
 var dist_team2_1 : float;
 var dist_team2_2 : float;
 var dist_team2_3 : float;
 
 var dist_team3_1 : float;
 var dist_team3_2 : float;
 var dist_team3_3 : float;
 
 var dist_noteam_1 : float;
 var dist_noteam_2 : float;
 var dist_noteam_3 : float;
 var dist_noteam_4 : float;
 var dist_noteam_5 : float;
 var dist_noteam_6 : float;
 var dist_noteam_7 : float;
 var dist_noteam_8 : float;
 var dist_noteam_9 : float;
 
 var team1aliveenear_1 : boolean = false;
 var team1aliveenear_2 : boolean = false;
 var team1aliveenear_3 : boolean = false;
 
 var team2aliveenear_1 : boolean = false;
 var team2aliveenear_2 : boolean = false;
 var team2aliveenear_3 : boolean = false;
 
 var team3aliveenear_1 : boolean = false;
 var team3aliveenear_2 : boolean = false;
 var team3aliveenear_3 : boolean = false;
 
 var teamnoallyaliveenear_1 : boolean = false;
 var teamnoallyaliveenear_2 : boolean = false;
 var teamnoallyaliveenear_3 : boolean = false;
 var teamnoallyaliveenear_4 : boolean = false;
 var teamnoallyaliveenear_5 : boolean = false;
 var teamnoallyaliveenear_6 : boolean = false;
 var teamnoallyaliveenear_7 : boolean = false;
 var teamnoallyaliveenear_8 : boolean = false;
 var teamnoallyaliveenear_9 : boolean = false;
 
 var enemynear  : boolean = false;
 
 function Start () {
 teamMan = GameObject.Find("Managerteam");
 
 }
 
 function Update () {
 
 PlayerList1 = teamMan.gameObject.GetComponent(manageteam).PlayerList1;
 PlayerList2 = teamMan.gameObject.GetComponent(manageteam).PlayerList2;
 PlayerList3 = teamMan.gameObject.GetComponent(manageteam).PlayerList3;
 PlayerListnoally = teamMan.gameObject.GetComponent(manageteam).PlayerListnoally;
 
 enemynear = this.gameObject.GetComponent(clicktomove).nearenemy;
 
 distlimit = Vector3.Distance(this.transform.position,limit.transform.position);
 
 if(PlayerList1.Count ==1)
 {
     dist_team1_1 = Vector3.Distance(this.transform.position,PlayerList1[0].transform.position);
 }
 if(PlayerList1.Count ==2)
 {
     dist_team1_1 = Vector3.Distance(this.transform.position,PlayerList1[0].transform.position);
     dist_team1_2 = Vector3.Distance(this.transform.position,PlayerList1[1].transform.position);
 }
 if(PlayerList1.Count ==3)
 {
     dist_team1_1 = Vector3.Distance(this.transform.position,PlayerList1[0].transform.position);
     dist_team1_2 = Vector3.Distance(this.transform.position,PlayerList1[1].transform.position);
     dist_team1_3 = Vector3.Distance(this.transform.position,PlayerList1[2].transform.position);
 }
 
 //////////////////////////////////////////////////
 
 if(PlayerList2.Count ==1)
 {
     dist_team2_1 = Vector3.Distance(this.transform.position,PlayerList2[0].transform.position);
 }
 if(PlayerList2.Count ==2)
 {
     dist_team2_1 = Vector3.Distance(this.transform.position,PlayerList2[0].transform.position);
     dist_team2_2 = Vector3.Distance(this.transform.position,PlayerList2[1].transform.position);
 }
 if(PlayerList2.Count ==3)
 {
     dist_team2_1 = Vector3.Distance(this.transform.position,PlayerList2[0].transform.position);
     dist_team2_2 = Vector3.Distance(this.transform.position,PlayerList2[1].transform.position);
     dist_team2_3 = Vector3.Distance(this.transform.position,PlayerList2[2].transform.position);
 }
 
 /////////////////////////////////////////////////
 
 if(PlayerList3.Count ==1)
 {
     dist_team3_1 = Vector3.Distance(this.transform.position,PlayerList3[0].transform.position);
 }
 
 if(PlayerList3.Count ==2)
 {
     dist_team3_1 = Vector3.Distance(this.transform.position,PlayerList3[0].transform.position);
     dist_team3_2 = Vector3.Distance(this.transform.position,PlayerList3[1].transform.position);
 }
 
 if(PlayerList3.Count ==3)
 {
     dist_team3_1 = Vector3.Distance(this.transform.position,PlayerList3[0].transform.position);
     dist_team3_2 = Vector3.Distance(this.transform.position,PlayerList3[1].transform.position);
     dist_team3_3 = Vector3.Distance(this.transform.position,PlayerList3[2].transform.position);
 }
 
 //////////////////////////////////////////////////
 
 if(PlayerListnoally.Count ==1)
 {
     dist_noteam_1 = Vector3.Distance(this.transform.position,PlayerListnoally[0].transform.position);
 }
 if(PlayerListnoally.Count ==2)
 {
     dist_noteam_1 = Vector3.Distance(this.transform.position,PlayerListnoally[0].transform.position);
     dist_noteam_2 = Vector3.Distance(this.transform.position,PlayerListnoally[1].transform.position);
 }
 if(PlayerListnoally.Count ==3)
 {
     dist_noteam_1 = Vector3.Distance(this.transform.position,PlayerListnoally[0].transform.position);
     dist_noteam_2 = Vector3.Distance(this.transform.position,PlayerListnoally[1].transform.position);
     dist_noteam_3 = Vector3.Distance(this.transform.position,PlayerListnoally[2].transform.position);
 }
 if(PlayerListnoally.Count ==4)
 {
     dist_noteam_1 = Vector3.Distance(this.transform.position,PlayerListnoally[0].transform.position);
     dist_noteam_2 = Vector3.Distance(this.transform.position,PlayerListnoally[1].transform.position);
     dist_noteam_3 = Vector3.Distance(this.transform.position,PlayerListnoally[2].transform.position);
     dist_noteam_4 = Vector3.Distance(this.transform.position,PlayerListnoally[3].transform.position);
 }
 if(PlayerListnoally.Count ==5)
 {
     dist_noteam_1 = Vector3.Distance(this.transform.position,PlayerListnoally[0].transform.position);
     dist_noteam_2 = Vector3.Distance(this.transform.position,PlayerListnoally[1].transform.position);
     dist_noteam_3 = Vector3.Distance(this.transform.position,PlayerListnoally[2].transform.position);
     dist_noteam_4 = Vector3.Distance(this.transform.position,PlayerListnoally[3].transform.position);
     dist_noteam_5 = Vector3.Distance(this.transform.position,PlayerListnoally[4].transform.position);
 }
 if(PlayerListnoally.Count ==6)
 {
     dist_noteam_1 = Vector3.Distance(this.transform.position,PlayerListnoally[0].transform.position);
     dist_noteam_2 = Vector3.Distance(this.transform.position,PlayerListnoally[1].transform.position);
     dist_noteam_3 = Vector3.Distance(this.transform.position,PlayerListnoally[2].transform.position);
     dist_noteam_4 = Vector3.Distance(this.transform.position,PlayerListnoally[3].transform.position);
     dist_noteam_5 = Vector3.Distance(this.transform.position,PlayerListnoally[4].transform.position);
     dist_noteam_6 = Vector3.Distance(this.transform.position,PlayerListnoally[5].transform.position);
 }
 if(PlayerListnoally.Count ==7)
 {
     dist_noteam_1 = Vector3.Distance(this.transform.position,PlayerListnoally[0].transform.position);
     dist_noteam_2 = Vector3.Distance(this.transform.position,PlayerListnoally[1].transform.position);
     dist_noteam_3 = Vector3.Distance(this.transform.position,PlayerListnoally[2].transform.position);
     dist_noteam_4 = Vector3.Distance(this.transform.position,PlayerListnoally[3].transform.position);
     dist_noteam_5 = Vector3.Distance(this.transform.position,PlayerListnoally[4].transform.position);
     dist_noteam_6 = Vector3.Distance(this.transform.position,PlayerListnoally[5].transform.position);
     dist_noteam_7 = Vector3.Distance(this.transform.position,PlayerListnoally[6].transform.position);
 }
 if(PlayerListnoally.Count ==8)
 {
     dist_noteam_1 = Vector3.Distance(this.transform.position,PlayerListnoally[0].transform.position);
     dist_noteam_2 = Vector3.Distance(this.transform.position,PlayerListnoally[1].transform.position);
     dist_noteam_3 = Vector3.Distance(this.transform.position,PlayerListnoally[2].transform.position);
     dist_noteam_4 = Vector3.Distance(this.transform.position,PlayerListnoally[3].transform.position);
     dist_noteam_5 = Vector3.Distance(this.transform.position,PlayerListnoally[4].transform.position);
     dist_noteam_6 = Vector3.Distance(this.transform.position,PlayerListnoally[5].transform.position);
     dist_noteam_7 = Vector3.Distance(this.transform.position,PlayerListnoally[6].transform.position);
     dist_noteam_8 = Vector3.Distance(this.transform.position,PlayerListnoally[7].transform.position);
 }
 if(PlayerListnoally.Count ==9)
 {
     dist_noteam_1 = Vector3.Distance(this.transform.position,PlayerListnoally[0].transform.position);
     dist_noteam_2 = Vector3.Distance(this.transform.position,PlayerListnoally[1].transform.position);
     dist_noteam_3 = Vector3.Distance(this.transform.position,PlayerListnoally[2].transform.position);
     dist_noteam_4 = Vector3.Distance(this.transform.position,PlayerListnoally[3].transform.position);
     dist_noteam_5 = Vector3.Distance(this.transform.position,PlayerListnoally[4].transform.position);
     dist_noteam_6 = Vector3.Distance(this.transform.position,PlayerListnoally[5].transform.position);
     dist_noteam_7 = Vector3.Distance(this.transform.position,PlayerListnoally[6].transform.position);
     dist_noteam_8 = Vector3.Distance(this.transform.position,PlayerListnoally[7].transform.position);
     dist_noteam_9 = Vector3.Distance(this.transform.position,PlayerListnoally[8].transform.position);
 }
 
 
 
 
 
 
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
 
 
 if(dist_team1_1 < distlimit)
 {
     if(PlayerList1[0].gameObject.GetComponent(healthbar).health >0)
     {
         team1aliveenear_1 = true;
     }
     else
     {
         team1aliveenear_1 = false;
     }
 }
 else if(dist_team1_1 > distlimit)
 {
     team1aliveenear_1 = false;
 }
 
 if(dist_team1_2 < distlimit)
 {
     if(PlayerList1[1].gameObject.GetComponent(healthbar).health >0)
     {
         team1aliveenear_2 = true;
     }
     else
     {
         team1aliveenear_2 = false;
     }
 }
 else if(dist_team1_2 > distlimit)
 {
     team1aliveenear_2 = false;
 }
 
 if(dist_team1_3 < distlimit )
 {
     if(PlayerList1[2].gameObject.GetComponent(healthbar).health >0)
     {
         team1aliveenear_3 = true;
     }
     else
     {
         team1aliveenear_3 = false;
     }
 }
 else if(dist_team1_3 > distlimit)
 {
     team1aliveenear_3 = false;
 }
 
 
 
 ////WORKS UP THIS LINE
 
 
 
 //////////////////////////////////////////////////////////
 
 
 ////DON'T WORKS UNDER THIS LINE
 
 
 
 
 if(dist_team2_1 < distlimit)
 {
     if(PlayerList2[0].gameObject.GetComponent(healthbar).health >0)
     {
         team2aliveenear_1 = true;
     }
     else
     {
         team2aliveenear_1 = false;
     }
 }
 else if(dist_team2_1 > distlimit)
 {
     team2aliveenear_1 = false;
 }
 
 if(dist_team2_2 < distlimit)
 {
     if(PlayerList2[1].gameObject.GetComponent(healthbar).health >0)
     {
         team2aliveenear_2 = true;
     }
     else
     {
         team2aliveenear_2 = false;
     }
 }
 else if(dist_team2_2 > distlimit)
 {
     team2aliveenear_2 = false;
 }
 
 if(dist_team2_3 < distlimit )
 {
     if(PlayerList2[2].gameObject.GetComponent(healthbar).health >0)
     {
         team2aliveenear_3 = true;
     }
     else
     {
         team2aliveenear_3 = false;
     }
 }
 else if(dist_team2_3 > distlimit)
 {
     team2aliveenear_3 = false;
 }
 
 
 
 
 
 
 
 /////////////////////////////////////////////////////////////////
 
 
 
 
 
 
 
 if(dist_team3_1 < distlimit)
 {
     if(PlayerList3[0].gameObject.GetComponent(healthbar).health >0)
     {
         team3aliveenear_1 = true;
     }
     else
     {
         team3aliveenear_1 = false;
     }
 }
 else if(dist_team3_1 > distlimit)
 {
     team3aliveenear_1 = false;
 }
 
 if(dist_team3_2 < distlimit)
 {
     if(PlayerList3[1].gameObject.GetComponent(healthbar).health >0)
     {
         team3aliveenear_2 = true;
     }
     else
     {
         team3aliveenear_2 = false;
     }
 }
 else if(dist_team3_2 > distlimit)
 {
     team3aliveenear_2 = false;
 }
 
 if(dist_team3_3 < distlimit )
 {
     if(PlayerList3[2].gameObject.GetComponent(healthbar).health >0)
     {
         team3aliveenear_3 = true;
     }
     else
     {
         team3aliveenear_3 = false;
     }
 }
 else if(dist_team3_3 > distlimit)
 {
     team3aliveenear_3 = false;
 }
 
 
 
 
 
 
 //////////////////////////////////////////////////////////////////////////
 
 
 
 
 
 
 
 
 if(dist_noteam_1 < distlimit)
 {
     if(PlayerListnoally[0].gameObject.GetComponent(healthbar).health >0)
     {
         teamnoallyaliveenear_1 = true;
     }
     else
     {
         teamnoallyaliveenear_1 = false;
     }
 }
 else if(dist_noteam_1 > distlimit)
 {
     teamnoallyaliveenear_1 = false;
 }
 
 //**********************************************
 
 if(dist_noteam_2 < distlimit)
 {
     if(PlayerListnoally[1].gameObject.GetComponent(healthbar).health >0)
     {
         teamnoallyaliveenear_2 = true;
     }
     else
     {
         teamnoallyaliveenear_2 = false;
     }
 }
 else if(dist_noteam_2 > distlimit)
 {
     teamnoallyaliveenear_2 = false;
 }
 
 //**********************************************
 
 if(dist_noteam_3 < distlimit)
 {
     if(PlayerListnoally[2].gameObject.GetComponent(healthbar).health >0)
     {
         teamnoallyaliveenear_3 = true;
     }
     else
     {
         teamnoallyaliveenear_3 = false;
     }
 }
 else if(dist_noteam_3 > distlimit)
 {
     teamnoallyaliveenear_3 = false;
 }
 
 //**********************************************
 
 if(dist_noteam_4 < distlimit)
 {
     if(PlayerListnoally[3].gameObject.GetComponent(healthbar).health >0)
     {
         teamnoallyaliveenear_4 = true;
     }
     else
     {
         teamnoallyaliveenear_4 = false;
     }
 }
 else if(dist_noteam_4 > distlimit)
 {
     teamnoallyaliveenear_4 = false;
 }
 
 //**********************************************
 
 if(dist_noteam_5 < distlimit)
 {
     if(PlayerListnoally[4].gameObject.GetComponent(healthbar).health >0)
     {
         teamnoallyaliveenear_5 = true;
     }
     else
     {
         teamnoallyaliveenear_5 = false;
     }
 }
 else if(dist_noteam_5 > distlimit)
 {
     teamnoallyaliveenear_5 = false;
 }
 
 //**********************************************
 
 if(dist_noteam_6 < distlimit)
 {
     if(PlayerListnoally[5].gameObject.GetComponent(healthbar).health >0)
     {
         teamnoallyaliveenear_6 = true;
     }
     else
     {
         teamnoallyaliveenear_6 = false;
     }
 }
 else if(dist_noteam_6 > distlimit)
 {
     teamnoallyaliveenear_6 = false;
 }
 
 //**********************************************
 
 if(dist_noteam_7 < distlimit)
 {
     if(PlayerListnoally[6].gameObject.GetComponent(healthbar).health >0)
     {
         teamnoallyaliveenear_7 = true;
     }
     else
     {
         teamnoallyaliveenear_7 = false;
     }
 }
 else if(dist_noteam_7 > distlimit)
 {
     teamnoallyaliveenear_7 = false;
 }
 
 //**********************************************
 
 if(dist_noteam_8 < distlimit)
 {
     if(PlayerListnoally[7].gameObject.GetComponent(healthbar).health >0)
     {
         teamnoallyaliveenear_8 = true;
     }
     else
     {
         teamnoallyaliveenear_8 = false;
     }
 }
 else if(dist_noteam_8 > distlimit)
 {
     teamnoallyaliveenear_8 = false;
 }
 
 //**********************************************
 
 if(dist_noteam_9 < distlimit)
 {
     if(PlayerListnoally[8].gameObject.GetComponent(healthbar).health >0)
     {
         teamnoallyaliveenear_9 = true;
     }
     else
     {
         teamnoallyaliveenear_9 = false;
     }
 }
 else if(dist_noteam_9 > distlimit)
 {
     teamnoallyaliveenear_9 = false;
 }
 
 ///////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 if(this.gameObject.GetComponent(clicktomove).ally1)
 {
     if(!this.gameObject.GetComponent(clicktomove).tryhide)
         {
             if(team2aliveenear_1 || team2aliveenear_2 || team2aliveenear_3 || team3aliveenear_1 || team3aliveenear_2 || team3aliveenear_3 ||
                teamnoallyaliveenear_1 || teamnoallyaliveenear_2 || teamnoallyaliveenear_3 || teamnoallyaliveenear_4 || teamnoallyaliveenear_5 ||
                teamnoallyaliveenear_6 || teamnoallyaliveenear_7 || teamnoallyaliveenear_8 || teamnoallyaliveenear_9)
                {
                        this.gameObject.GetComponent(clicktomove).nearenemy = true;
                }
         }
         else
         {
                 this.gameObject.GetComponent(clicktomove).nearenemy = false;
         }
 }
  ////////////////////////////////////////
 if(this.gameObject.GetComponent(clicktomove).ally2)
 {
     if(!this.gameObject.GetComponent(clicktomove).tryhide)
         {
             if(team1aliveenear_1 || team1aliveenear_2 || team1aliveenear_3 || team3aliveenear_1 || team3aliveenear_2 || team3aliveenear_3 ||
                teamnoallyaliveenear_1 || teamnoallyaliveenear_2 || teamnoallyaliveenear_3 || teamnoallyaliveenear_4 || teamnoallyaliveenear_5 ||
                teamnoallyaliveenear_6 || teamnoallyaliveenear_7 || teamnoallyaliveenear_8 || teamnoallyaliveenear_9)
                {
                        this.gameObject.GetComponent(clicktomove).nearenemy = true;
                }
         }
         else
         {
                 this.gameObject.GetComponent(clicktomove).nearenemy = false;
         }
 }
 //////////////////////////////////////////
 if(this.gameObject.GetComponent(clicktomove).ally3)
 {
     if(!this.gameObject.GetComponent(clicktomove).tryhide)
         {
             if(team1aliveenear_1 || team1aliveenear_2 || team1aliveenear_3 || team2aliveenear_1 || team2aliveenear_2 || team2aliveenear_3 ||
                teamnoallyaliveenear_1 || teamnoallyaliveenear_2 || teamnoallyaliveenear_3 || teamnoallyaliveenear_4 || teamnoallyaliveenear_5 ||
                teamnoallyaliveenear_6 || teamnoallyaliveenear_7 || teamnoallyaliveenear_8 || teamnoallyaliveenear_9)
                {
                        this.gameObject.GetComponent(clicktomove).nearenemy = true;
                }
         }
         else
         {
                 this.gameObject.GetComponent(clicktomove).nearenemy = false;
         }
 }
 //////////////////////////////////////////////
 if(this.gameObject.GetComponent(clicktomove).noally)
 {
     if(!this.gameObject.GetComponent(clicktomove).tryhide)
         {
             if(team1aliveenear_1 || team1aliveenear_2 || team1aliveenear_3 || team2aliveenear_1 || team2aliveenear_2 || team2aliveenear_3 ||
                team3aliveenear_1 || team3aliveenear_2 || team3aliveenear_3)
                {
                        this.gameObject.GetComponent(clicktomove).nearenemy = true;
                }
         }
         else
         {
                 this.gameObject.GetComponent(clicktomove).nearenemy = false;
         }
 }
 
 
 
 Hope in a help with this issue.
 Thanks and cheers

P.S Ah! i'm italian so please sorry for my english :)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Nov 23, 2015 at 08:20 PM 0
Share

I don't think the problem is your origin here...

Your script is way too long. You know it better than us so you should be able to narrow down a bit what is relevant. Also, you should state the issue.

So start with removing whatever is not relevant, describe what you want, what you get and what is wrong.

avatar image Michealunity fafase · Nov 23, 2015 at 09:26 PM 0
Share

Thanks for the answer, the script is too long because i made a workaround to get all distance. Originally i've made a short script that do the same of this script but the issue was that the script also if instruction are different get only the first distance and the other stay to 0, so i delete it. With this all distance are calculate but work partially and i don't know why. If i don't find a solution for this script i delete and create another. I have more patience :)

Thanks

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Michealunity · Dec 06, 2015 at 02:54 PM

Hello guys this is an update with my script for detect enemy. I have rewrite all and now works and it's more clean in my opinion. If can be useful for some people that have problem with this is my code:

//this script works with 3 team of 3 person and other person that are not in team

//tryhide is a variable that change layer in my pg (like LOL when you go into the wood) make it visible or not

//limit is an object that decide my distance of detect

 var PlayerList1 : List.<GameObject> = new List.<GameObject>();
 var PlayerList2 : List.<GameObject> = new List.<GameObject>();
 var PlayerList3 : List.<GameObject> = new List.<GameObject>();
 var PlayerListnoally : List.<GameObject> = new List.<GameObject>();
 
 var limit : GameObject;
 var distlimit : float;
 
 var collidersInRange : Collider [];
 
 var enemynear : boolean = false;
 
 function Update () {
 
 enemynear = this.gameObject.GetComponent(clicktomove).nearenemy;
 
 distlimit = Vector3.Distance(this.transform.position,limit.transform.position);
 
 
 collidersInRange = Physics.OverlapSphere(transform.position,distlimit);
 
 
 for(var currentgameobject : Collider in collidersInRange)
 {
     
     if(currentgameobject.tag == "Cube")
         {
             if(currentgameobject.gameObject.GetComponentInParent(clicktomove).ally1)
             {
                 if(!currentgameobject.GetComponentInParent(clicktomove).tryhide)
                     {
                         if(currentgameobject.gameObject.GetComponentInParent(healthbar).health >0)
                         {
                             if(!PlayerList1.Contains(currentgameobject.gameObject))
                             {
                                 PlayerList1.Add(currentgameobject.gameObject);
                                 PlayerList2.Remove(currentgameobject.gameObject);
                                 PlayerList3.Remove(currentgameobject.gameObject);
                                 PlayerListnoally.Remove(currentgameobject.gameObject);
                             }
                         }
                         else
                             {
                                 PlayerList1.Remove(currentgameobject.gameObject);
                                 PlayerList2.Remove(currentgameobject.gameObject);
                                 PlayerList3.Remove(currentgameobject.gameObject);
                                 PlayerListnoally.Remove(currentgameobject.gameObject);
                             }
                     }
                 else
                 {
                         PlayerList1.Remove(currentgameobject.gameObject);
                         PlayerList2.Remove(currentgameobject.gameObject);
                         PlayerList3.Remove(currentgameobject.gameObject);
                         PlayerListnoally.Remove(currentgameobject.gameObject);
                 }
             }
             
             if(currentgameobject.gameObject.GetComponentInParent(clicktomove).ally2)
             {
                 if(!currentgameobject.GetComponentInParent(clicktomove).tryhide)
                     {
                         if(currentgameobject.gameObject.GetComponentInParent(healthbar).health >0)
                         {
                             if(!PlayerList2.Contains(currentgameobject.gameObject))
                             {
                                 PlayerList2.Add(currentgameobject.gameObject);
                                 PlayerList1.Remove(currentgameobject.gameObject);
                                 PlayerList3.Remove(currentgameobject.gameObject);
                                 PlayerListnoally.Remove(currentgameobject.gameObject);
                             }
                         }
                         else
                             {
                                 PlayerList1.Remove(currentgameobject.gameObject);
                                 PlayerList2.Remove(currentgameobject.gameObject);
                                 PlayerList3.Remove(currentgameobject.gameObject);
                                 PlayerListnoally.Remove(currentgameobject.gameObject);
                             }
                     }
                 else
                 {
                         PlayerList1.Remove(currentgameobject.gameObject);
                         PlayerList2.Remove(currentgameobject.gameObject);
                         PlayerList3.Remove(currentgameobject.gameObject);
                         PlayerListnoally.Remove(currentgameobject.gameObject);
                 }
             }
             
             if(currentgameobject.gameObject.GetComponentInParent(clicktomove).ally3)
             {
                 if(!currentgameobject.GetComponentInParent(clicktomove).tryhide)
                     {
                         if(currentgameobject.gameObject.GetComponentInParent(healthbar).health >0)
                         {
                             if(!PlayerList3.Contains(currentgameobject.gameObject))
                             {
                                 PlayerList3.Add(currentgameobject.gameObject);
                                 PlayerList1.Remove(currentgameobject.gameObject);
                                 PlayerList2.Remove(currentgameobject.gameObject);
                                 PlayerListnoally.Remove(currentgameobject.gameObject);
                             }
                         }
                         else
                             {
                                 PlayerList1.Remove(currentgameobject.gameObject);
                                 PlayerList2.Remove(currentgameobject.gameObject);
                                 PlayerList3.Remove(currentgameobject.gameObject);
                                 PlayerListnoally.Remove(currentgameobject.gameObject);
                             }
                     }
                 else
                 {
                         PlayerList1.Remove(currentgameobject.gameObject);
                         PlayerList2.Remove(currentgameobject.gameObject);
                         PlayerList3.Remove(currentgameobject.gameObject);
                         PlayerListnoally.Remove(currentgameobject.gameObject);
                 }
             }
             
             if(currentgameobject.gameObject.GetComponentInParent(clicktomove).noally)
             {
                 if(!currentgameobject.GetComponentInParent(clicktomove).tryhide)
                     {
                         if(currentgameobject.gameObject.GetComponentInParent(healthbar).health >0)
                         {
                             if(!PlayerListnoally.Contains(currentgameobject.gameObject))
                             {
                                 PlayerListnoally.Add(currentgameobject.gameObject);
                                 PlayerList1.Remove(currentgameobject.gameObject);
                                 PlayerList2.Remove(currentgameobject.gameObject);
                                 PlayerList3.Remove(currentgameobject.gameObject);
                             }
                         }
                         else
                             {
                                 PlayerList1.Remove(currentgameobject.gameObject);
                                 PlayerList2.Remove(currentgameobject.gameObject);
                                 PlayerList3.Remove(currentgameobject.gameObject);
                                 PlayerListnoally.Remove(currentgameobject.gameObject);
                             }
                     }
                 else
                 {
                         PlayerList1.Remove(currentgameobject.gameObject);
                         PlayerList2.Remove(currentgameobject.gameObject);
                         PlayerList3.Remove(currentgameobject.gameObject);
                         PlayerListnoally.Remove(currentgameobject.gameObject);
                 }
 
             }
 
         }
 }
 
 for(var player1 : GameObject in PlayerList1)
 {
     var dist1 : float = Vector3.Distance (this.transform.position,player1.transform.position);
     if(dist1 > distlimit)
     {
         PlayerList1.Remove(player1.gameObject);
     }
 }
 
 for(var player2 : GameObject in PlayerList2)
 {
     var dist2 : float = Vector3.Distance (this.transform.position,player2.transform.position);
     if(dist2 > distlimit)
     {
         PlayerList2.Remove(player2.gameObject);
     }
 }
 
 for(var player3 : GameObject in PlayerList3)
 {
     var dist3 : float = Vector3.Distance (this.transform.position,player3.transform.position);
     if(dist3 > distlimit)
     {
         PlayerList3.Remove(player3.gameObject);
     }
 }
 
 
 for(var playerno : GameObject in PlayerListnoally)
 {
     var distno : float = Vector3.Distance (this.transform.position,playerno.transform.position);
     if(distno > distlimit)
     {
         PlayerListnoally.Remove(playerno.gameObject);
     }
 }
 
 
 
     if(this.gameObject.GetComponent(clicktomove).ally1)
     {
         if(PlayerList2.Count > 0|| PlayerList3.Count > 0|| PlayerListnoally.Count > 0)
             {
                 this.gameObject.GetComponent(clicktomove).nearenemy = true;
             }
         else
             {
                 this.gameObject.GetComponent(clicktomove).nearenemy = false;
             }
     }
 
     if(this.gameObject.GetComponent(clicktomove).ally2)
     {
         if(PlayerList1.Count > 0|| PlayerList3.Count > 0|| PlayerListnoally.Count > 0)
             {
                 this.gameObject.GetComponent(clicktomove).nearenemy = true;
             }
         else
             {
                 this.gameObject.GetComponent(clicktomove).nearenemy = false;
             }
     }
 
     if(this.gameObject.GetComponent(clicktomove).ally3)
     {
         if(PlayerList2.Count > 0|| PlayerList1.Count > 0|| PlayerListnoally.Count > 0)
             {
                 this.gameObject.GetComponent(clicktomove).nearenemy = true;
             }
         else
             {
                 this.gameObject.GetComponent(clicktomove).nearenemy = false;
             }
     }
 
     if(this.gameObject.GetComponent(clicktomove).noally)
     {
         if(PlayerList1.Count > 0|| PlayerList2.Count > 0|| PlayerList3.Count > 0)
             {
                 this.gameObject.GetComponent(clicktomove).nearenemy = true;
             }
         else
             {
                 this.gameObject.GetComponent(clicktomove).nearenemy = false;
             }
     }
 
 }


Maybe is not best script for performance but works for me.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Suggestions for AI Selecting From Multiple Targets? 3 Answers

Unity 3D enemy stop distance? 1 Answer

Target distance would only update closer 1 Answer

When the enemy character shoots, the bullet won't go to the position of my player! 2 Answers

How do I make my enemy stop moving towards me within a certain distance? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges