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 mrZimaus · Jun 17, 2014 at 09:01 PM · coroutinenewpong

Method keeps looping when using waitForSeconds

Hello newbie here. I am making a Pong like game and having some truble with it. The thing i want to do to make a pause before the ball fly away. I use waitForSeconds for this. This causes the method i wanted to make a pause before running to repeat. I would really appreciate some help Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Ball : MonoBehaviour {
 
 public float ballSpeed;
     
     void Start () {
         //sends away the ball when the game starts
         StartCoroutine(Pause());
     }
     void OnCollisionEnter(Collision collide) {
         //in this method the code checks if the player who is hitting the ball is
         //going up or down and gives a extra force in the traveling way
         float force = 50.0F;
         if (collide.gameObject.name == "Player01") 
         {
             if (Input.GetKey(KeyCode.W))
             {
                 rigidbody.AddForce(Vector3.up * force);
             }
             else if (Input.GetKey(KeyCode.S))
             {
                 rigidbody.AddForce(Vector3.down * force);
             }
         }
 
         if (collide.gameObject.name == "Player02") 
         {
             if (Input.GetKey(KeyCode.O))
             {
                 rigidbody.AddForce(Vector3.up * force);
             }
             else if (Input.GetKey(KeyCode.L))
             {
                 rigidbody.AddForce(Vector3.down * force);
             }
         }
     }
     void Update () {
         //if ball is only going up and down this will restart it
         if (rigidbody.velocity.x >= (-1) && rigidbody.velocity.x <= (1)) {
             StartCoroutine(Pause());
         }
         //acts like a restart button
         else if (Input.GetKey(KeyCode.R)) {
             StartCoroutine(Pause());
         }
     }
     void RandomDirection () {
         //makes a pause before sending away the ball
         float Dir = Random.Range(1.0F, 2.0F);
         rigidbody.velocity = new Vector3(0, 0, 0);
         transform.position = new Vector3(0, 0, 0);
         if (Dir <= 1.5F) {
             rigidbody.AddForce(new Vector3(50, 0, 0) * ballSpeed);
         }
         else {
             rigidbody.AddForce(new Vector3(-50, 0, 0) * ballSpeed);
         }
         //this gives the ball a vertical movement also
         rigidbody.AddForce(new Vector3(0, Random.Range(20, -20), 0) * ballSpeed);
     }
     IEnumerator Pause () {
         yield return new WaitForSeconds(2);
         RandomDirection ();
     }
 }

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 rutter · Jun 17, 2014 at 09:03 PM

This bit:

 else if (Input.GetKey(KeyCode.R)) {
     StartCoroutine(Pause());
 }

GetKey returns true once per frame, if the key is pressed. If you hold the R key down for multiple frames (which is likely), Pause may end up called once for each of those frames. Even over a single second, this could produce dozens of calls.

You may instead want GetKeyDown, which returns true for only one frame (when the key is first pressed).

Likewise, you have velocity checks which might call Pause many, many times.

You might want some sort of "state" flag, to indicate that you're waiting to reset. If you're already in the process of resetting, you probably shouldn't start that process again.

Comment
Add comment · Show 3 · 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 mrZimaus · Jun 17, 2014 at 09:14 PM 0
Share

fixed the get$$anonymous$$eyDown thing but i dont really know how to go about fixing the velocity problem, any ideas? I forgot to write that it only resets the ball if its going to vertical and it wont go so vertical when RandomDirection runs (sorry if im asking to much)

avatar image rutter · Jun 17, 2014 at 09:24 PM 0
Share

I'd probably go for the state flag. Something like this:

  • Add a boolean, isResetting.

  • Before starting a reset, check if isResetting is true. If it is, do nothing; if it isn't, go ahead with the reset.

The following examples are equivalent:

 //example 1
 IEnumerator Pause () {
     if (!isResetting) {
         isResetting = true;
         yield return new WaitForSeconds(2);
         RandomDirection ();
         isResetting = false;
     }
 }

 //example 2
 IEnumerator Pause () {
     if (isResetting) return;
     
     isResetting = true;
     yield return new WaitForSeconds(2);
     RandomDirection ();
     isResetting = false;
 }
avatar image mrZimaus · Jun 17, 2014 at 09:35 PM 0
Share

OHH YESS IT WOR$$anonymous$$ED!! Thank you 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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Ball wont go faster 2 Answers

function Start () problem!! 3 Answers

I need help !!! -3 Answers

change to glow texture on mouse click 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