- Home /
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. :(
Thanks in advance!
Answer by aldonaletto · May 29, 2011 at 10:12 AM
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.
Aldo, you probably meant:
transform.position.y = y0+amplitude*$$anonymous$$athf.Sin(speed*time.time);
Otherwise you would have an error converting a float into a Vector3. By the way thanks for the hint!
Answer by Mister Jason · Apr 22, 2011 at 09:22 AM
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.
How accurate is time within Unity? I'd like to do something similar but based upon XY coords...
For example (pseudo code): If object > XY1, move down... if object < XY2, move up. I'd like to have an object repeatedly moving up and down... but not bouncing, no physics necessary.
Any ideas?
Sorry... I thought it would be better to bring up older posts rather than creating new ones that have already been discussed. Consider me learned ;-)
Next time just post your own question and provide a link to one that might be helpful to describe your problem.
Answer by Meltdown · May 29, 2011 at 08:11 AM
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...
http://lesterbanks.com/2010/05/unity-3d-animation-tutorials/
Wow, thanks, $$anonymous$$eltdown! I didn't even know Unity had an inbuilt animation editor! This will save me tonnes of scripting work with my attack waves! Cheers!
Haha, no problem. Each day I learn something new about Unity that surprises me too :p
Answer by tkamruzzaman · Nov 11, 2015 at 01:23 PM
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;
}
The code is good. I checked it. However on line 6 you want to insert/add this bit of code tempPos = transform.position;
This is needed if you have more then 1 floating object. In my test I saw that it mushed all the coins at (0,0,0) due to the fact that we never specified where they should go after we applied the floating up and down part.
Answer by Lad-Ty · Nov 27, 2017 at 05:44 PM
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;
}
}
}
Bro, you are the reason for my love for this community!. Thanks! 2 years later - still the first choice for my google.
BTW - I realy appreciate the words at the start in your comment, I'm also trying to reach into the lost souls of unity script explorers... by publishing cool generic scripts in a shape of " cut- 5 years later, here is my version for this ... :)"
BTW - if anyone looking for cool rotation script. here is a link: https://forum.unity.com/threads/simple-rotation-script-free.510303/