简体   繁体   English

如何使用字符控制器操纵Time.timeScale?

[英]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'. 我正在尝试做类似原型游戏SuperHot的事情,其中​​“时间只有在移动时移动”。

The methods I can think of involve manipulating Time.timeScale with the input, or the character controller's velocity. 我能想到的方法包括使用输入或字符控制器的速度来操纵Time.timeScale。

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? 用角色控制器的速度操纵timeScale是否可行?
  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. 实际上,当您减少timeScale ,它将具有循环依赖性。 Depending on the Input will be better but it will depend on the look and feel you expect. 取决于Input会更好,但这取决于您期望的外观。

When timeScale is set to zero the game is basically paused if all your functions are frame rate independent. 当timeScale设置为零时,如果您所有功能均与帧速率无关,则游戏基本上会暂停。

Except for realtimeSinceStartup, timeScale affects all the time and delta time measuring variables of the Time class. 除了realtimeSinceStartup之外,timeScale影响Time类的所有时间和增量时间测量变量。

If you lower timeScale it is recommended to also lower Time.fixedDeltaTime by the same amount. 如果降低timeScale,建议也将Time.fixedDeltaTime降低相同的量。

FixedUpdate functions will not be called when timeScale is set to zero. 当timeScale设置为零时,将不会调用FixedUpdate函数。

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. 由于Physics是最有优先FixedUpdate既要注意以上几点,除非你的游戏有很多违规行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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