1
votes

I'm working on a script, so I could detect when player is not moving for x seconds and load another scene accordingly.

If player started moving again after x seconds, then loading another scene should't be called.

I've tried using isSleeping function and delay it by including Coroutine with WaitForSeconds but it is still checking Rigidbody2D every frame. Is there any other way to check if Rigidbody2D hasn't moved for x seconds and only then load game over level, otherwise continue moving as before?

 using UnityEngine;
 using System.Collections;

 public class PlayerStop : MonoBehaviour {


     void Update() {

         if (GetComponent<Rigidbody2D>().IsSleeping()) {
             Application.LoadLevel(2);
         }
     }
 }

In addition I have a script, which enables me to draw lines (with mouse) and stop player's movement, however lines disappear after x seconds. So for example if I'd set lines to disappear after 1 second, I would like to check if Rigidbody2D stopped moving for 2 seconds and only then load game over scene. Otherwise do nothing as Rigidbody2D will continue moving again after line disappears.

2
Maybe on each frame, you can check the coordinates of the previous frame. Continue this for x seconds and if the previous coordinates was same as the current coordinate in all frames, go to next levelwingerse

2 Answers

2
votes

Try this

using UnityEngine;
using System.Collections;

public class PlayerStop : MonoBehaviour {

    float delay = 3f;
    float threshold = .01f;

    void Update() {

        if (GetComponent<Rigidbody2D>().velocity.magnitude < threshold * threshold)
            StartCoRoutine("LoadTheLevel");
    }

    IEnumerator LoadTheLevel()
    {
        float elapsed = 0f;

        while (GetComponent<Rigidbody2D>().velocity.magnitude < threshold * threshold)
        {
            elapsed += Time.deltaTime;
            if(elapsed >= delay)
            {
                Application.LoadLevel(2);
                yield break;
            }
            yield return null;
        }
        yield break;
    }
}
0
votes

You could try this...I'm not able to test this right now, so may need a little tweaking...

First some private variables:

private float _loadSceneTime;
private Vector3 _lastPlayerPosition;
private float _playerIdleDelay;

And in the Update method check if the player has moved:

private void Update()
{
    //  Is it time to load the next scene?
    if(Time.time >= _loadSceneTime)
    {
        //  Load Scene
    }
    else
    {
        //  NOTE: GET PLAYERS POSITION...THIS ASSUMES THIS 
        //  SCRIPT IS ON THE GAME OBJECT REPRESENTING THE PLAYER
        Vector3 playerPosition = this.transform.position;

        //  Has the player moved?
        if(playerPosition != _lastPlayerPosition)
        {
            //  If the player has moved we will attempt to load 
            //  the scene in x-seconds
            _loadSceneTime = Time.time + _playerIdleDelay;
        }

        _lastPlayerPosition = playerPosition;
    }
}