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 siaran · Apr 02, 2015 at 10:44 PM · coroutinewait

Move (and other actions) multiple characters 1 at a time?

Okay, so, I cannot quite figure this out. The concept is, I want to have multiple characters, give one orders, it will then execute those orders and the camera will follow it, and then the game will move on to the next character. (If you've ever played any turn-based tactical rpg such as Fire Emblem or Final Fantasy Tactics or something - basically, like that).

For example, let's say I have some AI player that has a reference to all its units, then I'd want to do something like

 public Unit[] myUnits;
 
 void GiveOrders(){
  foreach(Unit u in myUnits){
   //Do something to determine what unit should do,specifics dont really matter here
   //Order is some kind of class that contains an order for a unit that it can execute
   Order order = new Order(walkHere, attackTarget);
   u.ExecuteOrder(order);
   //Now I want to wait until this unit is done with its 
   //orders before I move on to the next one... somehow
  }
 }


and in Unit I suppose there would be something like

 bool ordersCompleted;
 NavMeshAgent agent;
 
 public void ExecuteOrder(Order order){
  //walk first then attack. Should probably put something in Order that tells 
  //if it should walk then attack or attack then walk, but again not 
  //really relevant here
  agent.SetDestination(order.walkTo);
  //Again here should wait until action is completed
  Attack(order.attackTarget);
  //and again wait until action is completed
  //then do something that tells the AI that all orders are executed
  //and this unit is done
  ordersCompleted = true;
 }
 
 void Attack(Unit enemy){
   //Some kind of attack method would be here
 }

I'm fairly sure I should do something with coroutines here, but I am not getting them right :(

So in other words what I want to do is 'begin action A -> action A calls action B -> put action A on hold, do action B -> B is completed -> continue with A'.

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

Answer by siaran · Apr 03, 2015 at 04:57 PM

Ok, I figured this one out, turns out coroutines were not the way to go... (at least, my solution doesn't use any)

it sort of goes like this:

in AI class:

 Unit[] myUnits;
 int currentlyExecturing;
     

  //call this when this AI player's turn starts
   public void StartTurn(){
                 //start at unit 0
             currentlyExecuting = 0;
             GiveOrder();
         }

 void Update(){
     //check if current unit orders are completed each frame
     if(myUnits[currentlyExecuting].ordersCompleted){
             //if so, give orders to the next unit or end turn if it was the last unit
         if(currentlyExecuting < myUnits.Length-1){
             currentlyExecuting++;
             GiveOrder();
         }else{
             EndTurn();
         }
     }
 }
 
 //Give order fucntion
 void GiveOrder(){
     Order o = new Order();
     //should do something to determine what the order 
     //actually is
     myUnits[currentlyExecuting].ExectureOrder(0);
 }


And in unit:

 bool ordersCompleted;
 
 void ExecuteOrder(Order orders){
   ordersCompleted = false;
 
   agent.SetDestination(orders.destination);
   walking = true;
    
 }
 
 void Update(){
  if(walking) Walk()
 }
 
 void Walk(){
 if(agent.remainingDistance <= agent.stoppingDistance){
  walking = false;
  ordersCompleted = true;
 }

}

Haven;t incorporated any attack action yet so these agents just do walking, but they do it sequentially which is what I wanted. So I'll mark this question as answered, but if anyone has a better way of doing this, I'd love to hear it.

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

19 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

Related Questions

Hold or Wait while Coroutine finishes 6 Answers

I want to create a coroutine that will run a series of functions when a set variable reaches its value. What would be the best way to do this? 0 Answers

WaitUntil and WaitWhile don't work 1 Answer

Sequentially wait for multiple coroutines to finish 0 Answers

WaitForSeconds Question 2 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