5 Minute Script: Floating Rotation Effect in Unity

I’m working on a new level for KittenVR and I wanted to share a really quick script to add a space-like floating effect for objects in your Unity scene. Attach this script to an object that has a RigidBody, and you’ll instantly grant your scene a nifty little anti-gravity effect for floating in place and rotating gently.

The class (a typical MonoBehaviour inherited class that I’ve called NoGravity) is written in C# below. We’ll start by defining several variables to manipulate the overall effect. This script will take an object and move it up and down along the Y axis, while also applying a rotation to the object so it appears to be slowly spinning in space. No, it’s probably not quite physically-accurate, but I’ll chalk it up to a fictional planet with strange variable gravity.

_distance: a public variable that we can change for each object the script is attached to. This is how far from the object’s original position that we’ll deviate for our floating

_top, _bottom: two vectors that define the start and end points for our floating object. These are calculated from the object’s initial position and the _distance variable

_percent: how far we have gone, between 0% and 100%. Note that this is in decimal format (between 0 and 1)

_speed: how quickly our object is floating

_direction: we’ll be using a custom enumerator ‘Direction’ to specify if we are moving upward or downward


// Public variable to set the distance
public float _distance;

// Variables for floating
 private Vector3 _top, _bottom;
 private float _percent = 0.0f;
 private float _speed = 0.1f;
 private Direction _direction;
// Define direction up and down
 enum Direction { UP, DOWN};

Our Start method will initialize our _direction variable and set our locations based on the current position and the distance we’ve specified.

// Set the direction to up, and the locations
 void Start () {
    _direction = Direction.UP;
    _top = new Vector3(transform.position.x,
                       transform.position.y + _distance,
                       transform.position.z);
    _bottom = new Vector3(transform.position.x,
                          transform.position.y - _distance,
                          transform.position.z);
 }

In our Update function, we’re going to call two other helpers to separate our floating and rotation logic:

void Update () {
     ApplyFloatingEffect();
     ApplyRotationEffect();
 }

The first of our two helper functions will handle our floating effect. We will first check what direction we are going in. If we’re moving up and our percent completed is less than 100% (1.0f) we will increase the percent it has moved, then use the Lerp function to continue moving up to the top of our desired position. In Unity, the Lerp function will let us specify a position between the first two elements of the function call. We’ll base ours on the percentage variable.

If our direction is down, we’ll reverse the Lerp direction and move the other way. When we hit 100% (1.0f), we’ll go ahead and reverse the direction to go the other way.

// Apply the floating effect between the given positions
 void ApplyFloatingEffect()
 {
   if (_direction == Direction.UP && _percent < 1)
   {
      _percent += Time.deltaTime * _speed;
      transform.position = Vector3.Lerp(_top, _bottom, _percent);
   }
   else if (_direction == Direction.DOWN && _percent < 1) 
   {
       _percent += Time.deltaTime * _speed; 
       transform.position = Vector3.Lerp(_bottom, _top, _percent); 
   }
   if (_percent >= 1)
   {
      _percent = 0.0f;
      if (_direction == Direction.UP)
      {
          _direction = Direction.DOWN;
      }
      else
      {
         _direction = Direction.UP;
      }
 }
}

The last line (and end of our script) is a one-line function to rotate your object around its center based on the Time.deltaTime increment. You can change the direction of your rotation by modifying the first variable in the function (I chose forward for this example, but you can explore the Vector3 documentation for other options).

// Apply a random rotation effect
 void ApplyRotationEffect()
 {
    transform.Rotate(Vector3.forward, Time.deltaTime * 25f);
 }

The entire script is up on GitHub here!

Related Posts

3 thoughts on “5 Minute Script: Floating Rotation Effect in Unity

  1. Hi Rita!

    You could definitely use Quaternions for this, but with Lerp, you’d also need to have the interval increment and the resulting code would have been longer than using the .Rotate() method. It’s definitely feasible though, and a great question! I’d have to look into it in more detail to compare the two in more depth and write out the full code for the Quaternion.Lerp() approach!

  2. i love this little snow-white kitty so much…and thanx for all the tutorials.

Leave a Reply

Your email address will not be published.