top of page

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;
    }
}

 
 

Recent Posts

See All

FAVORITES

Try playing Core Keeper and learn from it's sound design!

From the sound effect to the music - this is a masterpiece!

WHAT'S NEW?

New game on Steam and a new mobile video asset are soon to be published!!

I AM THINKING ABOUT

AI is on the rise. Being able to use it in the right cases and explaining the task correctly is no easy task but once you finish a full task without writing a line of code - your world will change!

CONTACT ME

Use any social media to contact me. For any question about programing and project I prefer using my Discord server, See you there!

© 2021 by Amit Klein. Nerd Head.

bottom of page