Logo
Effects

Rect Sway

A reactive pointer driven sway effect for UI elements

Overview

Rect Sway adds an interactive sway motion to UI elements when the pointer hovers over them. The element smoothly shifts toward the pointer's direction and returns to its original position when the pointer exits. This effect is ideal for adding subtle depth and responsiveness to buttons, cards, panels, or hero sections.

Preview

Usage

Add Rect Sway to any UI object with a RectTransform

Optionally assign:

  • swayObject if the animated object is different from the component owner
  • canvas and targetCamera for world space or camera space canvases

Adjust settings:

  • smoothness (higher = snappier, lower = softer)
  • swayIntensity for how far the object moves
  • enableXAxis / enableYAxis to restrict sway direction

The element will:

  • React only when hovered
  • Move toward the cursor based on intensity
  • Smoothly return to default position on exit

Properties

References

NameTypeDescription
canvasCanvasCanvas used to convert pointer coordinates to world or overlay space
targetCameraCameraUsed for Screen Space Camera and World Space canvas types
swayObjectRectTransformThe object that will animate. Defaults to the component's RectTransform

Settings

NameTypeDescription
useUnscaledTimeboolRuns sway using unscaled deltaTime
enableXAxisboolEnables horizontal sway
enableYAxisboolEnables vertical sway
smoothnessfloatControls how responsive the sway motion is
swayIntensityfloatControls how strongly the sway reacts to cursor movement

Public Methods

NameParametersDescription
SetSwayState(enabled)boolEnables or disables sway behavior manually
ResetSway()NoneReturns the object to its original anchored position

Code Example

RectSwayExample.cs
using UnityEngine;
using Evo.UI;

public class RectSwayExample : MonoBehaviour
{
    public RectSway sway;

    void Start()
    {
        // Enable both axes for full sway effect
        sway.enableXAxis = true;
        sway.enableYAxis = true;

        // Adjust smoothness (higher = more responsive)
        sway.smoothness = 14f;

        // Adjust how far it moves relative to pointer
        sway.swayIntensity = 1.4f;

        // Optional: use scaled time if preferred
        sway.useUnscaledTime = true;
    }

    // Manually enable or disable sway, for example when activating a menu
    public void ActivateSway(bool state)
    {
        sway.SetSwayState(state);
    }
}

On this page