Spawn AI when player is around (Unity3d/C#) -


i have script spawns enemies set positions (point spawn). wondering how able make them spawn when player is, example, within 50 meters spawn-point.

wander manager:

public class wanderingmanager : monobehaviour {      public transform[] wanderingpoints;      void getnewpos(gameobject target){         target.sendmessage("setnewwanderpos", wanderingpoints[random.range(0, wanderingpoints.length)].position, sendmessageoptions.dontrequirereceiver);     } } 

enemy manager script:

[requirecomponent (typeof (wanderingmanager))]

public class enemymanager: monobehaviour {      public int maxzombies = 7;     public float spawninterval = 5.0f;     public string zombiestag = "zombie", playertag = "player";     public gameobject[] zombieprefabs = null;      private gameobject player = null;     private arraylist zombies = new arraylist();     private float lasttime = -10.0f;     private bool loaded = false;     private wanderingmanager manager;     void start () {         manager = getcomponent<wanderingmanager>();         findplayer();     }      // update called once per frame     void lateupdate () {          if(player == null && loaded){             startcoroutine(restart());         }          if(time.time > lasttime){             for(int i=0; i<zombies.count; i++){                 if(zombies[i] == null){                     zombies.removeat(i);                 }             }              if(zombies.count > maxzombies){                 zombies.removeat(zombies.count - 1);             }else{                 transform point = manager.wanderingpoints[random.range(0, manager.wanderingpoints.length)];                 gameobject z = instantiate(zombieprefabs[random.range(0, zombieprefabs.length)], point.position, point.rotation * quaternion.euler(0.0f, random.range(0.0f, 180.0f), 0.0f)) gameobject;                 zombies.add(z);             }             lasttime = time.time + spawninterval;         }     }      void findplayer(){         gameobject newplayer = gameobject.findwithtag(playertag);         if(newplayer != null && !newplayer.name.contains("clone")){             player = newplayer;             loaded = true;         }else if(newplayer != null && newplayer.name.contains("clone")){             destroy(newplayer);             retrysearch();         }     }      void retrysearch(){         findplayer();     }      ienumerator restart(){          yield return new waitforseconds(10.0f);          scenemanager.loadscene(scenemanager.getactivescene().buildindex);     } } 

any appreciated. thanks!

just distance check. this:

if (vector3.distance(player.transform.position,enemyspawn.transform.position) < 50): {      spawnenemylogic(); } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -