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.
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;
}
}
}