- Home /
Stealth AI Script. If less than or equal to not working correctly. Any fixes ?
//STATUS
var alert : boolean;
var caution : boolean;
var normal : boolean;
var alertTimer : float = 30;
var cautionTimer : float = 30;
//CHASING
var statusGUI : GUIText;
//var telicLocation : Transform;
var guardSpawn = Transform;
var enemy : GameObject;
var enemyUnits;
var enemyAIScript;
function Start(){
enemyUnits = GameObject.FindGameObjectsWithTag("Enemy");
enemyAIScript = enemyUnits.gameObject.GetComponent("EnemyAI");
alert = false;
caution = false;
normal = true;
}
function Update(){
if(alert == true){
print ("alert");
caution = false;
normal = false;
print("ALERT");
statusGUI.guiText.text = "ALERT : "+alertTimer;
alertTimer -= Time.deltaTime;
enemyUnits.gameObject.SendMessage("HuntTelic");
enemyAIScript.active = false;
**if(alertTimer <= 0){
alertTimer = 0;
alert = false;
normal = false;
caution = true;
}**
}
if(caution == true){
alert = false;
normal = false;
print("CAUTION");
//Instantiate(enemy, guardSpawn.position, guardSpawn.rotation);
//Instantiate(enemy, guardSpawn.position, guardSpawn.rotation);
enemyAIScript.active = false;
statusGUI.guiText.text = "CAUTION : "+cautionTimer;
cautionTimer -= Time.deltaTime;
if(cautionTimer <= 0){
alert = false;
caution = false;
normal = true;
}
}
if(normal == true){
alert = false;
caution = false;
statusGUI.guiText.text = "";
enemyAIScript.active = true;
}
}
function ActivateAlert(){
alert = true;
}
Apologies if my script is hard to understand or convuluted. This script is for the game to determine what state the enemies are in - Alert Caution or Normal. If it's in alert a timer will countdown from 30. After it hits 0 the enemy state is meant to change to caution. This is meant to change the timer back to 30 with the words caution instead of alert. The part of the script that doesn't work is the highlighted part in bold. Instead of resetting the timer keeps counting down and going into the minus numbers while it stays in the alert state.
Anyone have any ideas how to fix this? Again the part that needs fixing is the bold section where the if else statement is. Thanks in advance - Stealth.
Answer by Owen-Reynolds · Jan 03, 2015 at 08:16 PM
You know very well that <=
is working properly. You obliquely note the problem yourself: the script is overly complicated. Just recheck the debugging vars, add better ones, redo indents and clean, isolate in a new scene (in case of other weird scripts,) change in the Inspector as it runs and check... .
But, most people would rewrite, combining caution/alert/normal into just one var for "emotional state." For example, string state="caution";
. Then you don't have to worry about setting all three vars. You can just say state="normal";
(or use numbers, or enums.)