You have a pretty simple solution. What your problem here is that you are not preserving the original y position. Essentially, you need to store the original y position in a variable, that would look like this: (Don't forget to set the variable in the Start() function.)
Vector2 floatY;
float originalY;
public float floatStrength;
void Start ()
{
this.originalY = this.transform.position.y;
}
void Update () {
floatY = transform.position;
floatY.y = (Mathf.Sin(Time.time) * floatStrength);
transform.position = floatY;
}
After that, I suggest that instead of directly setting the y value of the floatY to Mathf.Sin(Time.time) * floatStrength
, you set it based on the sum of that value and the original transform position. The final code should look something like this:
Vector2 floatY;
float originalY;
public float floatStrength;
void Start ()
{
this.originalY = this.transform.position.y;
}
void Update () {
floatY = transform.position;
floatY.y = originalY + (Mathf.Sin(Time.time) * floatStrength);
transform.position = floatY;
}
Finally, instead of storing floatY as a Vector, you could store it as a float. Alternatively, you could use transform.position
directly, doing something like this:
Vector2 floatY;
float originalY;
public float floatStrength;
void Start ()
{
this.originalY = this.transform.position;
}
void Update () {
/* Old code:
floatY = transform.position;
floatY.y = originalY + (Mathf.Sin(Time.time) * floatStrength);
transform.position = floatY;
*/
transform.position = new Vector2(transform.position.x,
originalY + (Math.Sinf(Time.time) * floatStrength));
}
Finally, the last step is to do some minor refactoring as to remove some errors that have popped up along the way. Math.Sin
(Note: not Math.Sinf
) yields a double
instead of the desired float
, so we have to explicitly convert that value. Next, we can remove the Vector2 floatY
completely. Note that this would need to actually be a Vector3 to store transform.position
, as that also has a z
component, if we were to actually have kept using that. Finally, we must fix that same problem in the Update()
function, and change:
transform.position = new Vector2(transform.position.x,
originalY + (Math.Sinf(Time.time) * floatStrength));
to:
transform.position = new Vector3(transform.position.x,
originalY + (Math.Sinf(Time.time) * floatStrength), transform.position.z);
That code segment does not implement the other changes that I mentioned. In the end the complete code is as follows:
using UnityEngine;
using System;
using System.Collections;
public class FloatBehavior : MonoBehaviour
{
float originalY;
public float floatStrength = 1; // You can change this in the Unity Editor to
// change the range of y positions that are possible.
void Start()
{
this.originalY = this.transform.position.y;
}
void Update()
{
transform.position = new Vector3(transform.position.x,
originalY + ((float)Math.Sin(Time.time) * floatStrength),
transform.position.z);
}
}
Note that this code can be attached to an object either through New Object >> 2D >> Sprite
and then by adding this code as a script, or through the following code programmatically:
GameObject object = new GameObject();
object.name = "Floating Box";
object.AddComponent<SpriteRenderer>().sprite = SOME_SPRITE;
object.AddComponent<FloatBehavior>();
Here's an example of what this could look like:

Edit: Fixed compile errors