Levitating Orb

using System.Collections;
using UnityEngine;

public class Levitate : MonoBehaviour 
   public float amplitude;      
   public float speed;                
   private float startHeight;
   private Vector3 tempPos;
   void Start () 
       startHeight = transform.position.y;
   void Update () 
      tempPos = transform.position;
      tempPos.y = startHeight  + amplitude * Mathf.Sin(speed * Time.time);
      transform.position = tempPos;

I’ve been working on a new 2.5D platformer in which I have a little witch that creates a levitating orb to light her path. This script creates the levitating effect. It is attached to the orb which is a child of the witch in the hierarchy. ‘amplitude’ controls the amplitude of the sine curve. ‘speed’ controls the speed at which the orb oscillates. ‘startHeight’ sets the Y position of the bottom of the sine curve. ‘tempPos’ holds the temporary position of the orb. For a gentle effect, in the inspector I have the amplitude set at 0.3 and the speed set at 1.5.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s