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
1
Question by Jerlovescake · Jun 02, 2012 at 06:44 PM · javascriptailoop

AI isn't looping (Javascript)

Hi! So I'm scripting the AI for the final boss in my game, and I'm having an issue with the script not looping properly. When the player enters a certain distance from the boss, he sets the "state" variable of the boss from 0 to 1. When the state is 1, the boss performs his function, and when he finishes he sets the state to 2. When the state is 2, the boss performs the function for that state, and sets it to 3. etc. Once the boss gets to state 6 (the final state), he does his function then sets the state back to 1 so it will repeat. But once it sets the state back to 1, the boss doesn't do anything. Why is this happening?

var state : int;

//To trigger the boss battle function OnTriggerEnter (other : Collider) { if(other.gameObject.tag == "shootAtMe") { state = 1; //step one if (state == 1) { Shoot(); yield WaitForSeconds(4); state = 2; } //step two if (state == 2) { MoveLeft(); if (transform.position.x < -10) { state = 3; } } //step three if (state ==3) { Shoot(); yield WaitForSeconds(4); state = 4; } //step four if (state ==4) { MoveRight(); yield WaitForSeconds(4); state = 5; } //step five if (state == 5) { Shoot(); yield WaitForSeconds(4); state = 6; } //step six if (state ==6) { MoveLeft(); yield WaitForSeconds(2); moveSpeed = 0; state = 1; }

 }

}

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
Best Answer

Answer by aldonaletto · Jun 02, 2012 at 07:24 PM

The boss will wait for another OnTriggerEnter to repeat the sequence - if you exit and reenter the trigger it should work.
But you don't need these states - coroutines are great to implement state machines: just execute the instructions in sequence. WaitForSeconds will pause the coroutine for the desired intervals; the only weird thing is state 2, where it only passes to state 3 if the x coordinate is lower than -10; do you want it to repeat MoveLeft() until x < -10? Assuming that's the case, your AI could become something like this:

private var inRange = false; // tells when the player is inside range private var executing = false; // tells when the boss is executing its cycle

function OnTriggerEnter (other : Collider){ if(other.gameObject.tag == "shootAtMe"){ inRange = true; // player entered the trigger if (!executing) ExecuteBossCycle(); // if boss cycle not already executing, start it } }

function OnTriggerExit(other : Collider){ if(other.gameObject.tag == "shootAtMe"){ inRange = false; // player exited trigger - don't repeat cycle } }

function ExecuteBossCycle(){ executing = true; // cycle is executing while (inRange){ // repeat cycle while player in range //step one Shoot(); yield WaitForSeconds(4); //step two - don't know if this was what you're trying to do: while (transform.position.x >= -10){ MoveLeft(); // call MoveLeft while boss position.x >= -10 yield; // let Unity free till next frame } //step three Shoot(); yield WaitForSeconds(4); //step four MoveRight(); yield WaitForSeconds(4); //step five Shoot(); yield WaitForSeconds(4); //step six MoveLeft(); yield WaitForSeconds(2); moveSpeed = 0; // will repeat cycle if player inside range } executing = false; // cycle ended }

Comment
Add comment · Show 1 · 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 Jerlovescake · Jun 03, 2012 at 02:43 PM 0
Share

Oh my god! This works perfectly! Thank you so much. Your comment totally helped me understand coroutine so much better. Thank you so so so much!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Jump AI Help 0 Answers

create object and add increment number to it's name 3 Answers

Ai continues to move after calling it to stop 1 Answer

Modifying AI script to follow gameobject 1 Answer

ai navigation help 0 Answers


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