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 Seventh Stealth · Jan 03, 2015 at 06:48 PM · javascriptaitimertime.deltatimestealth

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.

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

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.)

Comment
Add comment · 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

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

26 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 avatar image avatar image avatar image

Related Questions

I cant get this timer to start when this bool turns true? 1 Answer

Pause & Unpause Different timers 1 Answer

My Object only moves when the variable ready is true. Problem is that ready is only ever true while update is running. So to move the object the variable always has to be true. How can I make it move if the variable ready is false. 1 Answer

How do I get a function to recognize a variable in function Update() 1 Answer

I am making an AI Script and i need to find out how to make the enemy move in random x and z axis movements(Random!!!)This script works, but the movement is never ending. 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