- Home /
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 ()
{
}
}
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.
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;
}
}
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.