Zombie Wander

I have this code that allows the zombie to wander with waypoints but when i have multiple zombies they tend to follow the same waypoint. I need them to follow their own spanwed point.

using UnityEngine;
using System.Collections;

public class Zombie : MonoBehaviour {
	private float time = 0f;
	private float range;
	private float time2 = 60f;
	private GameObject waypoint;
	private bool wander = true;
	private GameObject Player;
	private Health pHealth;

	public GameObject wayPre;
	public float health = 100f;
	public float zDamage = 7.5f;
	public float rotSpeed = 10f;
	public float Speed = 1f;
	//Function for loss of health by zombie
	public float zHealth (float pdamage){
		health = health - pdamage;
		return health;
	}

	void Start(){
		Player = GameObject.FindGameObjectWithTag ("Player");
		pHealth = Player.GetComponent<Health> ();
	}

	void FixedUpdate (){
		range = Random.Range (-50f, 100f);
	}

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

		if (Player != null) {
			float dis = Vector3.Distance (Player.transform.position, transform.position);

			waypoint = GameObject.FindGameObjectWithTag("Finish");

			time2 += Time.deltaTime;
			Debug.Log (time2);

			if (time2 >= 60f){
				Vector3 wayPos = new Vector3(transform.position.x + range, 0f, transform.position.z + range);
				GameObject wayClone = Instantiate(wayPre, wayPos, Quaternion.identity) as GameObject;
				time2 = 0f;
			}

			if (waypoint != null) {
				float dis2 = Vector3.Distance (waypoint.transform.position, transform.position);
				if (dis2 >=3f){
					wander = true;
					if (wander == true){
						transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (waypoint.transform.position - transform.position), rotSpeed * Time.deltaTime);
						transform.position += transform.forward * Speed * Time.deltaTime;
					}
				}
				else
					wander = false;
			}

			if (dis <= 2f) {
				time += Time.deltaTime;
				if (time >= 2f) {
					time = 0f;
					pHealth.newHealth (zDamage);

				}
			}
		}
		if (health <= 0f)
			Destroy (gameObject);
	
		if (time2 >= 30f)
			Destroy (waypoint);
	}
}

I think your problem is that you’re instantiating a Waypoint Clone in line 46, but then in your wander script you end up using your initially found waypoint. If I were you I would break all your update code into separate methods, it makes it much easier to read and understand.

Find Initial: waypoint = GameObject.FindGameObjectWithTag("Finish");

Make new: Vector3 wayPos = new Vector3(transform.position.x + range, 0f, transform.position.z + range); GameObject wayClone = Instantiate(wayPre, wayPos, Quaternion.identity) as GameObject;

What you end up using:

float dis2 = Vector3.Distance (waypoint.transform.position, transform.position);
                if (dis2 >=3f){
                    wander = true;
                    if (wander == true){
                        transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (waypoint.transform.position - transform.position), rotSpeed * Time.deltaTime);
                        transform.position += transform.forward * Speed * Time.deltaTime;
                    }

if you change the stuff you use to calculate your position and rotation from waypoint to wayClone you should be fine.