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.
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
| Name | Type | Description |
|---|---|---|
canvas | Canvas | Canvas used to convert pointer coordinates to world or overlay space |
targetCamera | Camera | Used for Screen Space Camera and World Space canvas types |
swayObject | RectTransform | The object that will animate. Defaults to the component's RectTransform |
Settings
| Name | Type | Description |
|---|---|---|
useUnscaledTime | bool | Runs sway using unscaled deltaTime |
enableXAxis | bool | Enables horizontal sway |
enableYAxis | bool | Enables vertical sway |
smoothness | float | Controls how responsive the sway motion is |
swayIntensity | float | Controls how strongly the sway reacts to cursor movement |
Public Methods
| Name | Parameters | Description |
|---|---|---|
SetSwayState(enabled) | bool | Enables or disables sway behavior manually |
ResetSway() | None | Returns the object to its original anchored position |
Code Example
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);
}
}