NavMesh Agent & Spawn [solved]

Hi guys, im trying to do a game like RE:Mercenaries. Where you controll a guy, and zombies will chase you in the whole map (without any range, whole map). So, i used this Script:

using UnityEngine;
using System.Collections;

public class followandhit : MonoBehaviour {
    //public Transform m_player;
    public Animator animador;
    Transform myTransfrom;
    //Transform target;
    bool camBlood;
    public float damage;
    public GameObject blood;
    //
    public float hitPoints=100f;
    public float currentHitPoints;
    public GameObject Blood;
    public NavMeshAgent navmesh;
     Transform target;


    // Use this for initialization
    void Awake(){

        myTransfrom = gameObject.transform;
        target = GameObject.FindWithTag("Player").transform;

        navmesh.enabled = false;
        }

    void Start () {

        animador =gameObject.GetComponent<Animator> ();

        currentHitPoints = hitPoints ;

 
        navmesh.enabled = true;




    }
 
    // Update is called once per frame
    void Update () {

        camBlood = false;
        //GetComponent<NavMeshAgent> ().destination = target.position;
        //navmesh.destination = target.position;
        navmesh.destination=target.position;
        if (Vector3.Distance(target.position, myTransfrom.position)>1.7)
               {
                     
                        animador.SetBool ("Follow", true);
                        animador.SetBool("Hit",false);
                           if(currentHitPoints<=0)
                                            {
                                                    navmesh.enabled=false;
                                                    animador.SetBool("Dead",true);
                                                    animador.SetBool ("Follow",false);
                                                    Destroy (gameObject,3); 
                                            }
                }
        if (Vector3.Distance (target.position, gameObject.transform.position) < 1.7)
                {
            animador.SetBool ("Follow",false);
            animador.SetBool ("Hit",true);

                            if(currentHitPoints<=0)
                                        {
                                        navmesh.enabled=false;
                                        animador.SetBool("Dead",true);
                                        animador.SetBool ("Follow",false);
                                        Destroy (gameObject,3); 
                                        }
                }

 
    }


    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag=="Player") {
            camBlood=true;
            healthPlayer h;
            h = collision.transform.GetComponent<healthPlayer> ();
            h.TakeDamagePlayer (damage);
            Instantiate (blood, collision.transform.position, collision.transform.rotation);
         
         
        }
    }

    public void TakeDamage(float amt){

        currentHitPoints -= amt;

 
    }

}

And my simple spawner script is this:

using UnityEngine;
using System.Collections;

public class SpawnZombie1 : MonoBehaviour {

    public GameObject zombie1;
    public float timeToSpawn;
    float contZomb=0f;

    // Use this for initialization
    void Start () {

        InvokeRepeating ("Spawn", 1, timeToSpawn);

    }

    // Update is called once per frame
    void Update () {





    }

    void Spawn()
    {

    Instantiate (zombie1, transform.position, transform.rotation);
                              
        
    }


}

So… i tried with 3 spawners, and it worked good. Now i want to make 6 more (its a triangle, 3 spawns on each side) and i got several lag when i move the main character. I think its due to navmesh.destination=target.position; where target.position as you saw is declared on Awake function to avoid this lag.

I called “lag” the delay of the zombies to react my movement, they have an angular speed or 120 so they have to rotate itselfs quickly, with 3 spawners they did that, but with more they dont.

Any suggestions to avoid that zombie´s rotation “lag” with more than 3 spawners?

I think the problem is everytime you set navmesh.destination it have to recalculate path and if you do that every frame it can be slow. I never played with this path thing, but perhaps you can just use some coroutine with WaitForSeconds.

1 Like

Thanks for the help Fraconte. I think i forgot one thing lol. I tried also adding on the update function a coroutine with WaitForSeconds of 0.2F,0.5F and 0.8F and didnt work either.

So maybe its Nav Mesh Agent glitchy on Unity 4.5? because it has no sense… i mean, with just 3 spawns works good, if i set active one more, the lag appear. And if i spawn each 2 seconds zombies with 3 spawners works good too :S . But if i spawn enemies each 45 secs with 4 spawners works wrong… So its not about the enemies on the screen. When i use 4 spawns the enemy spawns on the gameObject but it doesnt move at all, like he was blocked or so.

As you see in the script tried too many things like disable/enable the NavMesh Component etc etc…

Anyone knows another way of doing this? I tried with Rain, but the lack of help on their forum turns me of.

Another way of using “something” which autodetect the path and re-path it auto? Or a solution to this ?

“it seems” i solved this issue. How? I dont know.