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 /
avatar image
1
Question by izimonster7 · Jul 23, 2018 at 10:19 PM · loopinginfinitepong

Changing who serves in multiplayer pong

,So, I'm making a simple game of pong to help me learn Unity better, and I can't figure out how to switch which paddle gets to serve after each point scored. It's meant to have the ball follow on the paddle, then when the control button is pressed, it shoots the ball and cancels the while loop:

 void StartGame()
 {
     Serve();
 }
 public void Serve()
 {
     if (ServeVar == true)
     {
         this.transform.position = P1Paddle.transform.position + new Vector3(1f, 0f, 0f);
         while (ServeVar == true)
         {
             if (Input.GetKey(KeyCode.RightControl))
             {
                 ServeVar = false;
                 this.GetComponent<Rigidbody2D>().velocity = new Vector2(8f, -2f);
             }
         }
     }
     if (ServeVar == false)
     {
         this.transform.position = P2Paddle.transform.position + new Vector3(-1f, 0f, 0f);
         while (ServeVar == false)
         {
             if (Input.GetKey(KeyCode.LeftControl))
             {
                 ServeVar = true;
                 this.GetComponent<Rigidbody2D>().velocity = new Vector2(-8f, 2f);
             }
         }
     }
 }

Unity freezes, presumably because of an infinite while loop. I'm pretty sure there's something I'm missing here?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Nomenokes · Jul 23, 2018 at 10:29 PM

In Unity and most programming languages, code is executed mostly linearly. So the entire Update function of one object is run, then the next, and so on. That means your while loops are infinite because nothing else gets done while they run. The inputs aren't even tested because the entire program is stuck running the while loop.


Instead, you could do something like this:

  //the ServeVar class variable is no longer in use, delete it. It is now a local variable within the Serve method
  
  void Update(){
     if(Input.GetKey(KeyCode.LeftControl)) Serve(true);
     if(Input.GetKey(KeyCode.RightControl)) Serve(false);
 }

 public void Serve(bool ServeVar){ 
      //... your other code without the while loops, because the serve option has already been chosen
 }

The Update function works as a continuous loop until an option is chosen.

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 SoshJam · Jul 23, 2018 at 10:32 PM 1
Share

Oh, I was typing my solution when this came out. This one is way better.

avatar image
1

Answer by SoshJam · Jul 23, 2018 at 10:31 PM

Simple solution: Never use While loops. I can never get them to work.

Here's what you should do: Put this at the top of your code: public bool shouldServe; Then, in the Start function, replace Serve(); with shouldServe = true;

Just before the Serve() function, type this:

 void Update()
 {
     if (shouldServe)
     {
         Serve();
     }
 }


This tests if the Serve function should be ran, and if it should, then run it.

Next, delete all the while loops and corresponding brackets. Somewhere in your keypress statement, add shouldServe = false; so the game knows it doesn't have to serve anymore.

When you detect when the ball goes off-screen (and someone gets a point) remember to add shouldServe = true; so that it knows to serve again. If it is in a different script, reference it accordingly.

Hope this helps!

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 Nomenokes · Jul 23, 2018 at 10:33 PM 0
Share

Beat you by one $$anonymous$$ute, I know the feeling, sorry

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

88 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

Related Questions

Map looping 1 Answer

Stop animation after playing once 1 Answer

iTween: uneven looping with PutOnPath 1 Answer

Why is my shoot animation always looping? 1 Answer

Problem with seamless music looping in Unity 5 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