Floating Coin - Animation with a script
- thetperson314
- Aug 12, 2021
- 1 min read

Usually when wanting to make many animations of thing moving we will use an empty object. For simple animation like coin floating movement we are better off using a simple script for the animation.
(for more complicated things we won't use is because it takes computing power and when we have a lot of object it can hurt the game performance).
The Script:
using UnityEngine;
public class CoinHoverScript : MonoBehaviour
{
public float amplitude = 0.5f; //how much the coin goes up and down
public float frequency = 1f; //how much time it takes to complete a loop
//Position Storage Variables
Vector3 posOffset = new Vector3();
Vector3 tempPos = new Vector3();
private void Start()
{
// Store the starting position of the object
posOffset = transform.position;
}
private void Update()
{
tempPos = posOffset;
tempPos.y += Mathf.Sin(Time.fixedTime * Mathf.PI * frequency)* amplitude;
transform.position = tempPos;
}
}