Skip to main content

Over Time Helper

The OverTimeHelper class provides functionality for calculating value transitions over time using various motion methods. This class can be used in different scripts, not just node scripts, and is part of the Jungle.Extensions namespace.

Overview

The OverTimeHelper class includes functionalities for:

  • Calculating float, Vector2, Vector3, Quaternion, and Color values over time
  • Determining the progress of these values towards a target
  • Enabling or disabling the calculations
  • Choosing between different motion calculation methods

Properties

enabled

public bool enabled

True if this Over Time Helper is enabled. Used only for the inspector.

scaledTime

public bool scaledTime

Set true if the calculation should use scaled delta time.

rate

public float rate

Time scale rate.

method

public OverTimeHelper.Motion method

Motion calculation method.

IsDone

public bool IsDone { get; }

Returns true if the calculation is done or if the transition is not enabled.

Default

public static OverTimeHelper Default { get; }

Gets the default OverTimeHelper instance with Lerp motion and a rate of 1.


Methods

OverTimeHelper

public OverTimeHelper(bool enabled, bool scaledTime, float rate, Motion method, float precision = 0.01f)

Initializes a new instance of the OverTimeHelper class.

Parameters

  • enabled: Whether the helper is enabled.
  • scaledTime: Whether to use scaled delta time.
  • rate: The time scale rate.
  • method: The motion calculation method.
  • precision: The precision for determining if the calculation is done (default is 0.01f).

SetFloatOverTime

public float SetFloatOverTime(float currentValue, float targetValue)

Smoothly transitions the current float value towards the target float value over time using the specified motion method.

Parameters

  • currentValue: The current value.
  • targetValue: The target value.

Returns

The new float value after applying the transition for the current frame.

SetVector2OverTime

public Vector2 SetVector2OverTime(Vector2 currentValue, Vector2 targetValue)

Smoothly transitions the current Vector2 value towards the target Vector2 value over time using the specified motion method.

Parameters

  • currentValue: The current value.
  • targetValue: The target value.

Returns

The new Vector2 value after applying the transition for the current frame.

SetVector3OverTime

public Vector3 SetVector3OverTime(Vector3 currentValue, Vector3 targetValue)

Smoothly transitions the current Vector3 value towards the target Vector3 value over time using the specified motion method.

Parameters

  • currentValue: The current value.
  • targetValue: The target value.

Returns

The new Vector3 value after applying the transition for the current frame.

SetQuaternionOverTime

public Quaternion SetQuaternionOverTime(Quaternion currentValue, Quaternion targetValue)

Smoothly transitions the current Quaternion value towards the target Quaternion value over time using the specified motion method.

Parameters

  • currentValue: The current value.
  • targetValue: The target value.

Returns

The new Quaternion value after applying the transition for the current frame.

SetColorOverTime

public Color SetColorOverTime(Color currentValue, Color targetValue)

Smoothly transitions the current color value towards the target color value over time using the specified motion method.

Parameters

  • currentValue: The current color value.
  • targetValue: The target color value.

Returns

The new color value after applying the transition for the current frame.

GetFloatProgress

public static float GetFloatProgress(float current, float target, float original)

Returns the progress of a float value over time (0 - 1).

Parameters

  • current: The current value.
  • target: The target value.
  • original: The original value.

Returns

The progress as a float between 0 and 1.

GetVector2Progress

public static float GetVector2Progress(Vector2 current, Vector2 target, Vector2 original)

Returns the progress of a Vector2 value over time (0 - 1).

Parameters

  • current: The current value.
  • target: The target value.
  • original: The original value.

Returns

The progress as a float between 0 and 1.

GetVector3Progress

public static float GetVector3Progress(Vector3 current, Vector3 target, Vector3 original)

Returns the progress of a Vector3 value over time (0 - 1).

Parameters

  • current: The current value.
  • target: The target value.
  • original: The original value.

Returns

The progress as a float between 0 and 1.

GetQuaternionProgress

public static float GetQuaternionProgress(Quaternion current, Quaternion target, Quaternion original)

Returns the progress of a Quaternion value over time (0 - 1).

Parameters

  • current: The current value.
  • target: The target value.
  • original: The original value.

Returns

The progress as a float between 0 and 1.

GetColorProgress

public static float GetColorProgress(Color current, Color target, Color original)

Returns the progress of a Color value over time (0 - 1).

Parameters

  • current: The current color value.
  • target: The target color value.
  • original: The original color value.

Returns

The progress as a float between 0 and 1.


Enumerations

Motion

Represents the motion calculation methods.

  • Translate: Uses Mathf.MoveTowards or Vector3.MoveTowards.
  • Lerp: Uses Mathf.Lerp or Vector3.Lerp.
  • Slerp: Uses Quaternion.Slerp.
  • SmoothDamp: Uses Mathf.SmoothDamp or Vector3.SmoothDamp.

Example

Here is an example of how to use the OverTimeHelper class to smoothly transition a Vector3 value over time:

using System;
using Jungle;
using UnityEngine;

// Over Time Helper namespace
using Jungle.Extensions;

[NodeProperties(
Title = "Set Position",
Description = "Sets the inputted transform's position."
)]
[IONode(
InputPortName = "Set",
OutputPortName = "Done",
OutputPortType = typeof(Transform)
)]
public class SetPositionNode : IONode<Transform>
{
// The target position to set the transform to
[SerializeField]
private Vector3 position;

// The `OverTimeHelper` instance
[SerializeField]
private OverTimeHelper overTime = OverTimeHelper.Default;

private Transform _transform;
private Vector3 _originalPosition;

protected override void OnStart(Transform transform)
{
_transform = transform;

// Save the original position to a variable for the "GetProgress" method call
_originalPosition = _transform.position;
}

protected override void OnUpdate()
{
// Gradually updates the transform's position towards the target position
_transform.position = overTime.SetVector3OverTime(_transform.position, position);

// Sets `IsDone` to true when the current position matches the target position
if (overTime.IsDone)
{
CallAndStop(_transform);
}
}

public override float GetProgress()
{
// This will return a value between 0 (no progress) and 1 (complete progress)
return OverTimeHelper.GetVector3Progress(_transform.position, position, _originalPosition);
}
}