- Home /
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);
}
}
Answer by CScherer549 · Jul 20, 2014 at 07:26 PM
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.
Thanks for the help but i just this line of code.
Vector3 NewTarget(){
return new Vector3(transform.position.x + Random.Range(-25f, 50f), 0f, transform.position.z + Random.Range(-25f, 50f));
}
and fixed the wander script to this.
if (Player != null) {
float dis2 = Vector3.Distance (Target, transform.position);
if (wander == false){
time2 += Time.deltaTime;
Debug.Log(time2);
if (time2 >= Random.Range(10f, 20f)){
Target = NewTarget();
time2 = 0f;
}
}
if (wander == true) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (Target - transform.position), rotSpeed * Time.deltaTime);
transform.position += transform.forward * Speed * Time.deltaTime;
time2 += Time.deltaTime;
if (time2 >= 15f)
wander = false;
}
if (dis2 >=3f)
wander = true;
else
wander = false;
}
It is working perfectly the all follow their own pints now.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
Unity: C#: Eyes look at mouse: HELP!! 3 Answers
C# how to make Character Motor Jump Infinitely Upward 2 Answers