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?