Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by paru5611 · Feb 20, 2017 at 09:39 AM · aifixedupdatedemo

Where to start a game demo and how to stop it when a key pressed?

HI, I am new to unity. So far I am enjoying it and have been able to make a table tennis game. I have a script for moving the bats. Now that the game is working properly I want the script to follow three work flows: 1. Play demo as soon as game launched. 2. Player one will play with AI when the Enter key was pressed 3. Player one will play with Player two when Insert key was pressed.

To get the work flows done I started the AI for both bats and make them play each other in the FixedUpdate method. This serves the purpose demo play. To play with AI I started a coroutine inside update method which take Enter key input. And to play with another player I started another coroutine inside update method that takes Insert key as the input.

 void FixedUpdate(){
    //codes to play demo
 }
 
 IEnumerator PlayerOneWithAI (){
    // Codes for player one to play with AI
    Debug.Log("PlayerWithAI mode enabled");
 }
 
 IEnumerator PlayerOneWithPlayerTwo (){
   // Codes for two player play
   Debug.Log("PlayerOneWithPlayerTwo mode enabled");
 }
 
 void update(){ 
         if (Input.GetKey("enter"))
         {
             StartCoroutine("PlayerOneWithAI");
         }
         else if (Input.GetKey("insert"))
         {
             StartCoroutine("PlayerOneWithPlayerTwo");
         }
 }
 
 


Now when I play the game the demo starts fine. And when I pressed either Enter or Insert keys the demo dont stop. But I get those debug messages in the console. It seems the coroutines has started but the demo mode is not leaving the control of the bats from the FixedUpdate. What am I doing wrong? What could be the best way to get these work flows done? Also how am I supposed the start the demo again when the game is idle for some time? Thanks in advance for your helps.

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

Answer by LiloE · Feb 20, 2017 at 02:22 PM

It looks like the coroutines are overkill. What do you think of this classical solution?

     void FixedUpdate() {
         switch (state) {
             case State.Demo:
                 // start player1 in demo mode
                 // start player2 in demo mode
                 break;
             case State.OnePlayer:
                 // stop player1 if in demo mode
                 // start player2 in demo mode
                 break;
             case State.TwoPlayers:
                 // stop player1 if in demo mode
                 // stop player2 if in demo mode
                 break;
             default:
                 break;
         }

     }

     void update() {
         if (Input.GetKey("enter")) {
             state = State.OnePlayer;
         }
         else if (Input.GetKey("insert")) {
             state = State.TwoPlayers;
         }
     }
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 Lootheo · Feb 25, 2017 at 08:53 AM 0
Share

Nice solution (y)

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

129 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 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 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 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 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

AddForce and AI 1 Answer

How do I make AI objects not walk on top of each other? 5 Answers

top Down 2D flight game AI 0 Answers

Is there a way to implement a ghost mode where a ghost can replay the best possible solution of a set of data? 0 Answers

[Urgent] Finding Nearest Target 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