Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrIntel · Jul 19, 2014 at 11:52 PM · c#zombie

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);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrIntel · Jul 20, 2014 at 08:14 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges