- Home /
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 ();
}
}
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.
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)
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;
}
Your answer
Follow this Question
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