Object floating

I've got a simple question about making an object float up and down.

I have UI above my character, and I’m trying to get it to float up and down to give a better sense of movement. Any ideas?

I’ve searched for similar solutions, but haven’t gotten anything working. :frowning:

Thanks in advance!

If you just want a smooth floating movement, use the Mathf.Sin function:

// Save the y position prior to start floating (maybe in the Start function):

  var y0:float = transform.position.y;

// Put the floating movement in the Update function:

  transform.position.y = y0+amplitude*Mathf.Sin(speed*Time.time);

Since Sin ranges from -1 to +1, you’ll have the object floating from -amplitude to +amplitude around the y0 coordinate, and the time to complete one cycle is 2 * PI * speed - about 6.3 seconds if speed is 1.

I figure out!! :)

var floatup;
function Start(){
    floatup = false;
}
function Update(){
    if(floatup)
        floatingup();
    else if(!floatup)
        floatingdown();
}
function floatingup(){
    transform.position.y += 0.3 * Time.deltaTime;
    yield WaitForSeconds(1);
    floatup = false;
}
function floatingdown(){
    transform.position.y -= 0.3 * Time.deltaTime;;
    yield WaitForSeconds(1);
    floatup = true;
}

hope it will help.

You can also do something like this easily, and more advanced movements using Unity’s built-in animation editor.

Take a look at these videos…

In C# you can write something like this:

	public float amplitude;          //Set in Inspector 
	public float speed;                  //Set in Inspector 
	private float tempVal;
	private Vector3 tempPos;
	
	void Start () 
	{
		tempVal = transform.position.y;
	}

	void Update () 
	{		
		tempPos.y = tempVal + amplitude * Mathf.Sin(speed * Time.time);
		transform.position = tempPos;
	}

Good set up and worked, I had to make a couple changes…

using UnityEngine;
using System.Collections;

public class Floating : MonoBehaviour 

{

public float amplitude;          //Set in Inspector 
public float speed;                  //Set in Inspector 
public float tempVal;
public Vector3 tempPos;

void Start () 
{
	tempVal = transform.position.y;
}

void Update ()
{        
	tempPos.y = tempVal + amplitude * Mathf.Sin (speed * Time.time);
	transform.position = tempPos;

}

}

Since I was looking for a quick ‘floating’ script to save me some time and stumbled onto this topic on Google, but I still didn’t get exactly what I wanted (some ‘universal solution’), here is what I wrote afterwards to help other googling guys. Also includes ‘floating’ functionality for rotation and scale apart from position, all are togglable on/off:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SimpleFloating : MonoBehaviour {

	public bool animPos = true;
	public Vector3 posAmplitude = Vector3.one;
	public Vector3 posSpeed = Vector3.one;

	public bool animRot = true;
	public Vector3 rotAmplitude = Vector3.one*20;
	public Vector3 rotSpeed = Vector3.one;

	public bool animScale = false;
	public Vector3 scaleAmplitude = Vector3.one*0.1f;
	public Vector3 scaleSpeed = Vector3.one;

	private Vector3 origPos;
	private Vector3 origRot;
	private Vector3 origScale;

	private float startAnimOffset = 0;


	/**
	 * Awake
	 */
	void Awake() {
		origPos = transform.position;
		origRot = transform.eulerAngles;
		origScale = transform.localScale;
		startAnimOffset = Random.Range(0f, 540f);		// so that the xyz anims are already offset from each other since the start
	}
	
	/**
	 * Update
	 */
	void Update() {
		/* position */
		if(animPos) {
			Vector3 pos;
			pos.x = origPos.x + posAmplitude.x*Mathf.Sin(posSpeed.x*Time.time + startAnimOffset);
			pos.y = origPos.y + posAmplitude.y*Mathf.Sin(posSpeed.y*Time.time + startAnimOffset);
			pos.z = origPos.z + posAmplitude.z*Mathf.Sin(posSpeed.z*Time.time + startAnimOffset);
			transform.position = pos;
		}

		/* rotation */
		if(animRot) {
			Vector3 rot;
			rot.x = origRot.x + rotAmplitude.x*Mathf.Sin(rotSpeed.x*Time.time + startAnimOffset);
			rot.y = origRot.y + rotAmplitude.y*Mathf.Sin(rotSpeed.y*Time.time + startAnimOffset);
			rot.z = origRot.z + rotAmplitude.z*Mathf.Sin(rotSpeed.z*Time.time + startAnimOffset);
			transform.eulerAngles = rot;
		}

		/* scale */
		if(animScale) {
			Vector3 scale;
			scale.x = origScale.x * (1+scaleAmplitude.x*Mathf.Sin(scaleSpeed.x*Time.time + startAnimOffset));
			scale.y = origScale.y * (1+scaleAmplitude.y*Mathf.Sin(scaleSpeed.y*Time.time + startAnimOffset));
			scale.z = origScale.z * (1+scaleAmplitude.z*Mathf.Sin(scaleSpeed.z*Time.time + startAnimOffset));
			transform.localScale = scale;
		}
	}
}

This is the easiest way