FANS

A fan object that lifts players and trash, and the movement state that makes it work.

THE FAN

A fan blows a column of air upward. Anything with a Rigidbody2D inside the column is lifted: thrown objects, crates and players. I use it to build vertical levels where players float between platforms.

// FanLogic: everything a level designer can tune, right in the inspector.
[SerializeField, Range(10, 500)] private float _fanForce = 60f;
[SerializeField] private float _playerForceMultiplier = 2;
[SerializeField, CurveRange(0, 0, 1, 1)] private AnimationCurve _fanForceCurve;

// The fan is a trigger box. Its height is the length of the airflow.
_fanCollider.isTrigger = true;
_fanTriggerLength = _fanCollider.bounds.size.y;

THE AIRFLOW

While something is inside the trigger, the fan adds an upward impulse every physics step. The strength depends on how high the object is in the airflow.

// Every physics step, anything inside the trigger is pushed up.
private void OnTriggerStay2D(Collider2D other)
{
    if (other.TryGetComponent<Rigidbody2D>(out var rb) && _fanCollider != null && _isFanActive)
    {
        MakeItemFloat(other, rb);
    }
}

private void MakeItemFloat(Collider2D collider, Rigidbody2D rb)
{
    // 0 at the bottom of the fan, 1 at the top of the airflow.
    Vector2 bottomCenterCollider = _fanCollider.bounds.min;
    Vector2 itemPosition = collider.transform.position;
    var distance = Vector2.Distance(bottomCenterCollider, itemPosition);
    var relativeColliderLength = distance / _fanTriggerLength;

    var force = _fanForce * _fanForceCurve.Evaluate(relativeColliderLength);
    if (collider.CompareTag("Player")) force *= _playerForceMultiplier;

    rb.AddForce(transform.up * force, ForceMode2D.Impulse);
}

Why a curve

A constant force makes objects shoot out of the top. With a curve, the force can fade near the end of the airflow, so objects settle and hover. Designers can reshape it per fan without touching the code.

LIFTING PLAYERS

Players need more than a force. Their own movement code fights the lift, so the fan switches the player into a special InFan movement state when they enter, and turns off persistent inertia so old momentum does not fight the airflow.

// FanLogic: entering the airflow.
private void OnTriggerEnter2D(Collider2D other)
{
    if (other.TryGetComponent<Rigidbody2D>(out var rb) &&
        other.gameObject.layer == LayerMask.NameToLayer("Player") &&
        other.gameObject.TryGetComponent<PlayerMovementBehavior>(out var playerMovement))
    {
        playerMovement.SetIsInFan(true);
        if (playerMovement.HasPersistentInertia)
        {
            playerMovement.HasPersistentInertia = false;
        }
    }
}

While the player is in that state, the movement states that would keep them on the ground, or make them jump or slide, refuse to run.

// The fan puts the player in a dedicated movement state.
// While in it, ground, jump, slide and fast fall are all disabled.
{ PlayerMovementState.OnGround, () => !_isInFan },
{ PlayerMovementState.InFastFall, () => !_isInFan && !IsInPodium() },
{ PlayerMovementState.InSlide, () => !_isInFan },
{ PlayerMovementState.InJump, () => !_isInFan },
{ PlayerMovementState.InWallJump, () => !_isInFan },

if (_isInFan) { SetMovementState(PlayerMovementState.InFan); return; }

BUGS AND GUARDS

Three problems the fan code had to solve. The movement state for players was added by a teammate, so I explain it here as part of how the fan works.

Players stuck on the ground

A player who was standing or walking on the ground under a fan was not lifted, because ground movement kept control of the body. A dedicated InFan state fixes this: every movement state that fights the lift refuses to run while the player is in a fan.

// BUG: a player standing or walking on the ground was never lifted, because the
// ground movement states kept control of the body.
// FIX: a dedicated InFan state. Every state that fights the lift refuses to run
// while _isInFan is true (see the table above), and the fan switches the state itself.
playerMovement.SetIsInFan(true);
Overlapping fans

With two overlapping fans, leaving one would end the fan state even though the player is still inside the other. The movement code counts the fans the player is in, instead of using a simple on and off.

// BUG (prevented): two fans can overlap. Leaving the first one would end the
// fan state while the player is still inside the second.
// FIX: count the fans instead of using a boolean.
public void SetIsInFan(bool entering)
{
    _fanCount = Mathf.Max(0, _fanCount + (entering ? 1 : -1));

    var isInFan = _fanCount > 0;
    if (_isInFan == isInFan) return;      // nothing changed: do nothing

    _isInFan = isInFan;
    if (IsHit) return;                    // do not interrupt a hit reaction
    ...
}
A fan that breaks quietly

Forgetting the collider or the trigger checkbox on a fan used to be an easy mistake. The fan forces the collider to be a trigger and logs a clear error when the collider is missing.

// Guard: a fan without its box collider disables nothing silently, it says so.
void Start()
{
    _fanCollider = GetComponent<BoxCollider2D>();
    if (_fanCollider == null)
    {
        Log.Error("Fan Collider is not set");
    }
    else
    {
        _fanCollider.isTrigger = true;     // forgetting the checkbox cannot break the level
        _fanTriggerLength = _fanCollider.bounds.size.y;
    }
}