Search Unity

How do I make a sprite have a simple animation where it loops moving/bobbing up and down?

Discussion in '2D' started by FeldIV, Jan 20, 2022.

  1. FeldIV

    FeldIV

    Joined:
    Dec 16, 2018
    Posts:
    27
    Hello, I'm interested in making this little 'door' sprite icon loop in an up and down animation automatically, very simple looking. How do I do this effectively?

    Thank you!

    upload_2022-1-20_11-46-22.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    I would start here:

    Screen Shot 2022-01-20 at 3.21.04 PM.png
     
  3. FeldIV

    FeldIV

    Joined:
    Dec 16, 2018
    Posts:
    27
    None of those results are exactly what I was looking for, unfortunately.

    I'll keep trying; thank you for your help!
     
    mikadigitalfira likes this.
  4. FeldIV

    FeldIV

    Joined:
    Dec 16, 2018
    Posts:
    27
    With the help of some friends, I got working what I wanted in a quick and easy modular solution. If you want to have a given object bob up and down in a loop, stick this script on it;

    Code (CSharp):
    1. public class MoveLoop : MonoBehaviour
    2. {
    3.     public GameObject icon;
    4.     public float speed;
    5.  
    6.     public void Update()
    7.     {
    8.         float y = Mathf.PingPong(Time.time * speed, 0.5f);
    9.          icon.transform.localPosition = new Vector3(0, y, 0);
    10.     }
    11. }
     
    mikadigitalfira likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    You really owe it to yourself to see how easy it would be with an animation. No code to write, no syntax to learn, no code to maintain, and you can hand it off to your technical artist team to tweak it however they want and make that thing do all kinds of weird wiggly wobbly stuff.

    Animations are awesome in Unity.