Last active
October 9, 2021 21:58
-
-
Save oliverbooth/7d3e116fd1ab9cafb4f9 to your computer and use it in GitHub Desktop.
Floating-like objects in Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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