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 crunchyZ · Jan 09, 2014 at 04:58 PM · c#coroutines

Coroutine isn't entering If statement c#

Hi guys, i'm new to coroutines and im trying to make a simple Ai, it is supposed to follow when in range and lay a trail of nodes and navigate back to start position when the player breaks the range. it gets into the idle IEnumerator but wont then enter the If statement.

 using UnityEngine;
 using System.Collections;
 
 public class shogoth_follow : MonoBehaviour {
     public GameObject target;
     int range; 
     float lockPos = 0;
     bool follow;
     bool hasFollowed;
     float timer;
     bool timerActive;
     public GameObject[] path;
     Vector3 myPosition;
     public GameObject path_Node;
     float speed =  3.0f;
     private float startTime;
     Vector3 startRotation;
 
     void Start () 
     {
         target = GameObject.FindGameObjectWithTag ("Player");
         StartCoroutine("idle");
         range = 10;
         
     }
 
     IEnumerator idle()
     {
 
         if(Vector3.Distance(gameObject.transform.position,target.transform.position)< range)
         {
             Debug.Log ("seen");
             startRotation = transform.rotation.eulerAngles;
             follow = true;
             hasFollowed = true;
 
         }
         else if (Vector3.Distance(gameObject.transform.position,target.transform.position)> range)
         {
             follow = false;
             if (hasFollowed)
             {
                 CancelInvoke();
                 yield return StartCoroutine("backTrack");
                 
                 
             }
         }
         if (follow)
         {
             Debug.Log ("follow state");
             yield return StartCoroutine("chase");
             
         }
         yield return null;
     }
 
     void spawnNode () 
     {
         GameObject newNode = Instantiate (path_Node, transform.position, transform.rotation) as GameObject;
         newNode.name = "pathNode";
         Debug.Log ("path node spawned");
 
     }
 
 
 
     void FindPath()
     {
         int i;
         GameObject[] pathNodes = GameObject.FindGameObjectsWithTag("pathnode");
         path = new GameObject[pathNodes.Length];
         for (i = 0; i < pathNodes.Length; i ++)
         {
             path[i] = pathNodes[i];
         }
     }
 
     GameObject closestPath (GameObject[] targets)
     {
         int i;
         float closest = (path[0].transform.position - transform.position).sqrMagnitude;
         int targetNumber = 0;
         for (i = 1; i < targets.Length; i++)
         {
             float currentDistance = (path[i].transform.position - transform.position).sqrMagnitude;
             if(currentDistance < closest)
             {
                 closest = currentDistance;
                 targetNumber = i;
             }
         }
         return path[targetNumber];
     }
 
     IEnumerator backTrack()
     {
         int i;
         GameObject moveTarget;
         FindPath ();
         for (i = 0; i < path.Length; i++)
         {
             myPosition = transform.position;
             startTime = Time.time;
             moveTarget = closestPath(path);
 
 
             float step = speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards(transform.position, moveTarget.transform.position, step);
 
 
             Destroy(moveTarget);
             transform.rotation = Quaternion.FromToRotation(transform.rotation.eulerAngles,startRotation);
         }
         yield return StartCoroutine ("idle");
     }
 
     IEnumerator chase()
     {
         InvokeRepeating("spawnNode",0,3);
         transform.LookAt(target.transform.position);
         transform.rotation = Quaternion.Euler (lockPos,transform.rotation.eulerAngles.y ,lockPos);
         transform.Translate(Vector3.forward *  Time.deltaTime*4);
     }
 
     void Update () 
     {
 
     }
 }
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
1

Answer by Kolodej · Jan 09, 2014 at 05:26 PM

Hi, I think your problem is, that the Idle coroutine does only one iteration (nothing meets the conditions), therfore nothing is tested again (there is no loop in Idle() routine).

Note: Check the UNITYGEMS, there are articles about Coroutines, Finite State Machines, AI, ... with examples.

Comment
Add comment · Show 2 · 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 crunchyZ · Jan 09, 2014 at 06:44 PM 0
Share

ive added a loop to the idle but when i get within range to start the chase it just freezes the game, the new idle coroutine looks like this

 Enumerator idle()
     {
 
         while(true)
         {
             Debug.Log ("idle");
         if(Vector3.Distance(gameObject.transform.position,target.transform.position)< range)
         {
             
             startRotation = transform.rotation.eulerAngles;
             follow = true;
             hasFollowed = true;
 
         }
         else if (Vector3.Distance(gameObject.transform.position,target.transform.position)> range)
         {
             follow = false;
             if (hasFollowed)
             {
                 CancelInvoke();
                     isIdle = false;
                 yield return StartCoroutine("backTrack");
                 
                 
             }
         }
         if (follow)
         {
             Debug.Log ("follow state");
                 isIdle = false;
             yield return StartCoroutine("chase");
             
         }
         yield return null;
         }
     }
 
avatar image Kolodej · Jan 09, 2014 at 06:55 PM 0
Share

Hi, I am pretty sure that is because of you are starting a new coroutines each frame. You have to remember actual state and then decide what routine will you start (if any). That is why I have proposed to read the Finite State $$anonymous$$achines article on UnityGems.

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I repeat an action after set number of seconds, 4 Answers

Running a coroutine without StartCoroutine 1 Answer

How to spawn evenly spaced game objects and move them in a circular path? 1 Answer


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