wasicool2
wasicool2

Reputation: 193

How can I bypass Time.time scale

I have a script attached to my player that has IEnumerator OnTriggerExit2D in it. Within it there is a time.time scale that is equal to 0 , which is making the whole game pause after it collides with an tagged object. I set it that way so that my player will stop at a certain time after collision(I used yield return new WaitForSeconds).

My question is; Is there a way to make my player not affected by the time.time scale but still stop after collision occurs, so my player still functions while the whole game is still paused. Here is my script:

 public class Player : MonoBehaviour {

    public float speed = 15f;
    private Vector3 target;
    public Player playerMovementRef;
    void Start () {
        target = transform.position;
    }

    void Update () {
        if (Input.GetMouseButtonDown(0)) {
            target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            target.z = transform.position.z;
        }
        transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    }  

    IEnumerator OnTriggerExit2D (Collider2D other){
        if (other.tag == "Bouncy object")
            playerMovementRef.enabled = false;
        yield return new WaitForSeconds (0.35f);
        Time.timeScale = 0f;
        playerMovementRef.enabled = true;
    }
}

Upvotes: 1

Views: 2378

Answers (1)

Tod Chung
Tod Chung

Reputation: 21

Case1

You can remove Time.deltaTime from the Update. Update is still called even when you set timescale to 0. Only Time.deltaTime will be 0 so your player will not move.

You can put some constant value instead of using Time.deltaTime to move your player. Although it will not frame rate independent, you can make your player move.

Case2

You can disable(GetComponent().enabled=false) all the rigidbody from all the other gameobject except your player instead of setting Time.timescale to 0.

Upvotes: 2

Related Questions