简体   繁体   中英

How do I manipulate Time.timeScale with the character controller?

I'm trying to do something like that prototype game SuperHot, where 'time moves only when you move'.

The methods I can think of involve manipulating Time.timeScale with the input, or the character controller's velocity.

The following is how I did it:

using UnityEngine;
using System.Collections;

public class TimeMechanic : MonoBehaviour {
private float targetScale;

// Use this for initialization
void Start ()
{
    targetScale = 0.02f;
}

// Update is called once per frame
void Update ()
{
    Time.timeScale = targetScale; //setting the timeScale
    if (Input.GetAxis ("Horizontal") != 0 || Input.GetAxis ("Vertical") != 0)
    {
        targetScale += 0.03f; //incrementing the variable by 0.03
        if (targetScale > 1f)
        {
            targetScale = 1f; //limits the variable to 1 at max
        }
    }
    else
    {
        targetScale -= 0.03f; //decrementing the variable by 0.03
        if (targetScale < 0.02f)
        {
            targetScale = 0.02f; //limits variable to 0.02 at min
        }
    }
    Debug.Log (targetScale);
}

}

My questions:

  1. Is manipulating the timeScale with the character controller's velocity viable?
  2. Are there things I should improve in this code?

It will depend on your algorithms. In fact when you reduce the timeScale it will have a circular dependency. Depending on the Input will be better but it will depend on the look and feel you expect.

When timeScale is set to zero the game is basically paused if all your functions are frame rate independent.

Except for realtimeSinceStartup, timeScale affects all the time and delta time measuring variables of the Time class.

If you lower timeScale it is recommended to also lower Time.fixedDeltaTime by the same amount.

FixedUpdate functions will not be called when timeScale is set to zero.

As Physics are most preferred with a FixedUpdate it is necessary to note the above points unless your game will have a lot of irregularities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM