0
\$\begingroup\$

I want to create some sort of delay for the game to restarts when the ball is stops moving but the game keeps restarting whenever the velocity reaches zero. I want use a timer where the game will restarts when the ball is stopping for too long but I don't know how. Here's the code. The Restart() will be called from another script when the game starts and the ball will starts moving. Any help would be appreciated.

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class BallCondition : MonoBehaviour {

    // Use this for initialization
    public bool isLose = false;
    public Rigidbody2D rb;

    void Awake () {

        rb = GetComponent<Rigidbody2D>();   
    }
    public IEnumerator Restart() 
     {
        Debug.Log(rb.velocity);
        if((rb.velocity.x)==0 && (rb.velocity.y) == 0)
        {
            //print(Time.time); 
            Debug.Log("stopped");
            if((rb.velocity.x)==0 && (rb.velocity.y) == 0 && !isLose)
            {
                yield return new WaitForSeconds(3);
                Debug.Log("lose");
                isLose = true;
                SceneManager.LoadScene(0);  
            }
        }
     }
}

This is the script where I call the Restart()

using UnityEngine;
using System.Collections;

public class Force : MonoBehaviour {

    public float moveSpeed = 5;
    public static bool isAlreadyClicked = false;

    void Start() 
    {
       Reset();
    }

    void Update()
    {
        if(Input.GetMouseButton(1))
        {

            if(!isAlreadyClicked)
            {
                GetComponent<Rigidbody2D>().AddForce(new Vector2(moveSpeed,0));
                Debug.Log("clicked");
                isAlreadyClicked = true;
                BallCondition player = GetComponent<BallCondition>();
                StartCoroutine(player.Restart());       
            }       
        }

    }
    public void Reset()
    {
        isAlreadyClicked = false;
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ It would help to see the script where you're calling Restart. \$\endgroup\$
    – DMGregory
    Commented Oct 21, 2016 at 3:39
  • \$\begingroup\$ Edited the question, sorry about that. Hope it helps \$\endgroup\$ Commented Oct 21, 2016 at 3:44
  • \$\begingroup\$ Can you provide the Debug.Log result for further clarification? \$\endgroup\$
    – Geo Baby
    Commented Oct 21, 2016 at 10:35

1 Answer 1

1
\$\begingroup\$

Your code for Restart will only run once.

You could use something like:

public IEnumerator Restart()
{
    while (true)
    {
        Debug.Log("stopped");
        // if you want to limit the check by 1 per second
        yield return new WaitForSeconds(1f);
        if (rb.IsSleeping())
        {
            yield return new WaitForSeconds(3);
            Debug.Log("lose");
            SceneManager.LoadScene(0);
        }
    }
}

I've also changed the velocity check for rb.IsSleeping() (more info in Unity Docs)

I hope this helps! Good luck :)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Sorry for the late reply. This code works like a charm! I did manage to do it but your code runs perfectly like I how want it. Tbh I never knew IsSleeping() is exist. Thanks a lot XD \$\endgroup\$ Commented Oct 27, 2016 at 8:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .