- Home /
AI Case Switch Issue - Javascript, 2D Shooter
First the code:
function Update () {
player =
CheckStatus();
print ("Checking Status");
switch(status)
{
case AIStatus.Idle:
print ("Idling");
Idle();
break;
case AIStatus.Firing:
print("Activating Firing");
Firing();
break;
}
}
//does successfully switch between idle and scared one time, and one time only.
function CheckStatus () {
//dist = (player.position - this.transform.position).magnitude; //not updating for some reason
dist = (player.position.z - this.transform.position.z); //not updating for some reason
if (dist < fireDistance) {
status = AIStatus.Firing;
print ("AI is Firing at dist:" + dist);
}
if (dist > awareDistance) {
status = AIStatus.Idle;
print ("AI is Idle at dist: " + dist);
}
}
function Idle() {
}
function Firing() {
}
Now the issue:it appears that CheckStatus() never updates the dist variable, and always shows the initial distance. The result is it's always stuck in one state or the other. In other words - it's either always idling, or always firing.
Halp
edit: The issue was apparently with dist always reading negative. Solved by taking a negative of the whole equation.
Oh yeah! sorry, didn't notice that.
Thanks for looking out
Do the values of fireDistance and awareDistance overlap? If they don't, then it's possible that the calculated distance is bouncing back and forth against the endpoints of that outside range, thus never getting caught.
Answer by tokenshi · Jul 20, 2011 at 05:57 PM
gah, not sure what happened there, but it didn't copy the whole line. It should read:
player = GameObject.Find("Player(Clone)").transform;
Your answer

Follow this Question
Related Questions
Boids for 2D 0 Answers
object pauses at the each way points for a vary short time 1 Answer
Transform.translate change 1 Answer
Simple Sentry Guard Creation Help 2 Answers