Time

There are two ways to manipulate time:

  • Change the Time Scale within the Unity Time Manager. This changes the time scale for all objects within the scene.
  • Change the Time Scale within the Ultimate Character Locomotion. This will change the time scale for only the character. A negative value cannot be set.

When the time scale is changed on the character any objects spawned from the character will also have the same time scale. As an example if you throw a grenade when the character has a 0.5 time scale then the grenade will take twice as long as normal to move.

Events

When the character’s time scale changes the “OnChangeTimeScale” event will be sent from the built-in Event System. A corresponding Unity event will also be sent. No event will be sent when the time scale is changed through the Unity Time Manager. The following example will subscribe to this event from a new component:

using UnityEngine;
using Opsive.Shared.Events;

public class MyObject : MonoBehaviour
{
    /// <summary>
    /// Initialize the default values.
    /// </summary>
    public void Awake()
    {
        EventHandler.RegisterEvent<float>(gameObject, "OnCharacterChangeTimeScale", OnChangeTimeScale);
    }

    /// <summary>
    /// The character's local timescale has changed.
    /// </summary>
    /// <param name="timeScale">The new timescale.</param>
    private void OnChangeTimeScale(float timeScale)
    {
        Debug.Log("New time scale: " + timeScale);
    }

    /// <summary>
    /// The GameObject has been destroyed.
    /// </summary>
    public void OnDestroy()
    {
        EventHandler.UnregisterEvent<float>(gameObject, "OnCharacterChangeTimeScale", OnChangeTimeScale);
    }
}