Skip to content

Core.Utilities.TimerOff

Dennis Steffen edited this page Jan 4, 2026 · 1 revision

The file you are looking at is TimerOff.cs. This class implements a logic pattern commonly known in industrial automation and PLC programming as a Timer Off (TOF).

Explanation of TimerOff

The purpose of a TimerOff is to keep an output signal "True" for a specified duration after the input signal has switched from "True" to "False".

Think of it as a "shut-down delay." While the input is active, the output is active. Once the input stops, the timer starts counting down; the output only turns off once that countdown finishes.

How it works (Internal Logic):

  1. Input is True: The Output property is immediately set to true, and the internal timer (_elapsed) is reset to zero.
  2. Input switches to False: The timer begins to increment (using deltaTime).
  3. During the delay: While the timer is counting but hasn't reached the _delay limit, the Output remains true.
  4. Timer reaches Limit: Once _elapsed >= _delay, the Output finally switches to false.
  5. Interruption: If the input becomes true again before the timer finishes, the timer resets, and the Output stays true without ever having flickered off.

Use Cases

  1. Coyote Time (Platformers): In a platformer, you often want to allow the player to jump for a few milliseconds even after they have walked off a ledge. You would pass "IsGrounded" as the input; the Output tells you if the jump is still valid.
  2. UI Feedback/Tooltips: Preventing a tooltip from disappearing immediately when the mouse leaves an icon, allowing the user to move the mouse back in without the UI flickering.
  3. Combat Mechanics: Keeping a "Combo Window" active for a short duration after the last hit.
  4. Visual Effects: Keeping a "warning" light or highlight active for a moment after a specific event has ended.

Examples

Example 1: Coyote Time for Jumping

This example allows a player to jump for 150ms after leaving a platform.

public class PlayerController
{
    private TimerOff _coyoteTimer = new TimerOff(150f); // 150ms window

    public void Update(float deltaTime, bool isGrounded, bool jumpButtonPressed)
    {
        // Update the timer based on whether we are touching the floor
        _coyoteTimer.Update(isGrounded, deltaTime);

        // If the output is true, we are either on the ground 
        // OR we just left the ground less than 150ms ago.
        if (jumpButtonPressed && _coyoteTimer.Output)
        {
            PerformJump();
            _coyoteTimer.Reset(); // Clear timer after jumping
        }
    }

    private void PerformJump() { /* Jump logic */ }
}

Example 2: Interactive Prompt (Preventing Flickering)

If you have a prompt that appears when near an object, using a TimerOff prevents the prompt from flickering if the player is right on the edge of the detection range.

public class InteractionSystem
{
    private TimerOff _promptVisibilityTimer = new TimerOff(500f); // Keep visible for 0.5s

    public void Update(float deltaTime, bool isWithinRange)
    {
        _promptVisibilityTimer.Update(isWithinRange, deltaTime);

        if (_promptVisibilityTimer.Output)
        {
            ShowInteractionPrompt();
        }
        else
        {
            HideInteractionPrompt();
        }
    }

    private void ShowInteractionPrompt() { /* UI Logic */ }
    private void HideInteractionPrompt() { /* UI Logic */ }
}

Key Properties

  • Output: The current state (True if input is high OR if the delay is still running).
  • TimeRemaining: Useful for UI progress bars (e.g., showing how much "Coyote Time" or "Combo Time" is left).
  • Elapsed: How many milliseconds have passed since the input went false.

Clone this wiki locally