Skip to content

Instantly share code, notes, and snippets.

@oliverbooth
Last active October 9, 2021 21:58
Show Gist options
  • Select an option

  • Save oliverbooth/7d3e116fd1ab9cafb4f9 to your computer and use it in GitHub Desktop.

Select an option

Save oliverbooth/7d3e116fd1ab9cafb4f9 to your computer and use it in GitHub Desktop.
Floating-like objects in Unity
using UnityEngine;
/// <summary>
/// Represents a behavior script which will cause the object to float following a sine wave pattern.
/// </summary>
public class FloatObject : MonoBehaviour
{
[SerializeField] private float _amplitude = 0.25f;
[SerializeField] private float _frequency = 1f;
private float _t = 0f;
private float _y = 0f;
private void Start()
{
_y = transform.position.y;
}
private void Update()
{
_t = (_t + (Time.deltaTime * _frequency)) % (Mathf.PI * 2f);
var position = transform.position;
position.y = (Mathf.Sin(_t) * _amplitude) + __y;
transform.position = position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment