Skip to content

Game.Input.SmoothVector2Processor

Assembly: Assembly-CSharp
Namespace: Game.Input

Type: class

Base: SmoothProcessor

Summary:
SmoothVector2Processor provides exponential smoothing for Vector2 input values. It lerps the current value toward the previous value using a smoothing factor raised to the power of the provided delta, zeroes extremely small vectors to avoid jitter, and optionally scales the result by Time.deltaTime when configured to use time-based smoothing (via the base class's time flag). This processor is intended to be used as part of an input-processing pipeline (e.g., Unity Input System custom processor) and relies on smoothing/time configuration provided by its SmoothProcessor base class.


Fields

  • private const float kDelta
    Used as a very small threshold (1E-12f) to detect near-zero vectors. When a smoothed vector's squared magnitude falls below this constant, the value is clamped to Vector2.zero to avoid extremely small residual values that may cause jitter.

Properties

  • None declared on this class.
    Note: smoothing and time flags referenced here (m_Smoothing, m_Time) are defined in the SmoothProcessor base class; configure those on the processor (via the base class API) when using this class.

Constructors

  • public SmoothVector2Processor()
    No explicit constructor is defined in the source; the default parameterless constructor is used. No special initialization is required by this derived class.

Methods

  • protected override Vector2 Smooth(Vector2 value, ref Vector2 lastValue, float delta)
    Implements the smoothing algorithm for Vector2 values. Behavior:
  • If the base-class smoothing parameter (m_Smoothing) is greater than 0:
    • Compute t = Mathf.Pow(m_Smoothing, delta).
    • Interpolate value toward lastValue using Vector2.Lerp(value, lastValue, t). This performs exponential-like smoothing controlled by m_Smoothing and the passed delta.
    • If the resulting value's squared magnitude is below kDelta (1E-12f), set the value to Vector2.zero to avoid extremely small noise.
  • Update lastValue to the returned value (so subsequent calls use the latest smoothed state).
  • If the base-class time flag (m_Time) is true, multiply the final value by Time.deltaTime to convert into a time-scaled delta.
  • Return the smoothed (and possibly time-scaled) Vector2.

Parameters: - value: current raw input Vector2 to be smoothed. - lastValue: reference to the previous smoothed value; will be updated by the method. - delta: delta value used when computing the pow(m_Smoothing, delta) factor (often frame delta or other time step supplied by the caller).

Threading / performance notes: - Uses Mathf.Pow and Vector2.Lerp; lightweight, suitable for per-frame input processing. - Zeroing tiny vectors helps avoid passing extremely small floats to downstream logic.

Usage Example

// Typical usage: this processor is intended to be part of an input-processing pipeline
// (for example, registered with Unity's Input System as a custom processor).
// The Smooth method is protected, so the input system (or a derived helper) invokes it.

//
// Example: call the protected Smooth method from a small derived helper to apply smoothing manually
//
using UnityEngine;
using Game.Input;

public class SmoothVector2Helper : SmoothVector2Processor
{
    // Expose a public wrapper that allows manual invocation for testing or custom pipelines.
    public Vector2 Apply(Vector2 input, ref Vector2 last, float delta)
    {
        return Smooth(input, ref last, delta);
    }
}

// Usage in an update loop:
var helper = new SmoothVector2Helper();
// Configure smoothing/time if the base class exposes public properties (names depend on base implementation).
// helper.Smoothing = 0.9f; helper.Time = true; // example only — actual property names depend on SmoothProcessor<T>.

Vector2 last = Vector2.zero;
Vector2 rawInput = new Vector2(1f, 0f);
float delta = Time.deltaTime;

Vector2 smoothed = helper.Apply(rawInput, ref last, delta);

//
// Alternatively, when used as a custom processor in the Unity Input System,
// add/assign the processor in your Input Action asset:
// Processor string example: "SmoothVector2(smoothing=0.9,time=true)"
//