Skip to content

ahmedAwedni/Unity-floating-object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity Floating Object System

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

✨ Features

  • 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.

📦 Included Scripts

  • "FloatingObject3D.cs" - For standard 3D rendering and physics.
  • "FloatingObject2D.cs" - Optimized for Orthographic 2D rendering and 2D physics.

🎮 How to Use

  1. Attach the Script: Add either the 2D or 3D script to your GameObject.
  2. Setup the Motion: Adjust the Amplitude, Frequency, and Float Axis in the inspector.
  3. 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.
  4. Play: The object will gracefully bob up and down, and automatically destroy itself when collected!

🧠 Design Notes

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.


🚀 Possible Extensions

  • 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.

🛠 Unity Version

Tested in Unity6+ (should work in most modern versions).


📜 License

MIT

About

A lightweight and customizable floating animation component for 2D and 3D objects in Unity.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages