A lightweight, customizable, and event-driven floating animation component for 2D and 3D objects in Unity.
Ideal for:
- Collectable items (Coins, Health Potions)
- Power-ups
- Interactive quest objects
- Decorative environmental elements
- Adjustable Animation: Control the amplitude (float height), frequency (float speed), and the directional axis of the float.
- Built-In Pickup Triggers: Natively supports trigger detection to act as a collectible item, instantly firing "UnityEvents" upon collision.
- Optional Rotation: Easily toggle constant rotation for that classic 3D platformer feel.
- Random Phase Offset: Prevents synchronized "robotic" movement when multiple floating objects are spawned next to each other.
- Visual Editor Gizmos: Instantly see the exact path and limits of the floating object mapped out in your Scene View.
- "FloatingObject3D.cs" - For standard 3D rendering and physics.
- "FloatingObject2D.cs" - Optimized for Orthographic 2D rendering and 2D physics.
- Attach the Script: Add either the 2D or 3D script to your GameObject.
- Setup the Motion: Adjust the Amplitude, Frequency, and Float Axis in the inspector.
- Setup the Pickup (Optional):
- Check the "Enable Pickup" boolean.
- Ensure your GameObject has a "Collider" (or "Collider2D") set to Is Trigger.
- Make sure your Player object has the tag "Player".
- Link any logic (like playing a sound effect or granting score) into the "OnPickup" UnityEvent box.
- Play: The object will gracefully bob up and down, and automatically destroy itself when collected!
The floating motion uses a sine wave based on "Time.time" to ensure smooth and continuous movement that isn't dependent on physics frames. By baking the "UnityEvents" directly into the trigger phase, the object acts as a standalone mechanic—you do not need to write a messy collision check in your Player script to see if they bumped into a coin.
- Animation Curves: Swap the sine wave for an "AnimationCurve" to allow custom bobbing profiles with sharp drops or easing.
- Pooling Support: Swap the "Destroy(gameObject)" call with a Release method if you are using a modern Object Pooling system.
- Magnetic Pull: Add a trigger sphere that slowly lerps the object towards the player when they get close.
Tested in Unity6+ (should work in most modern versions).
MIT