UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"create coin object that has a looping 2d animation"
using UnityEngine; public class Coin : MonoBehaviour { public Sprite[] coinSprites; public float animationSpeed = 0.1f; private SpriteRenderer spriteRenderer; private int currentSpriteIndex = 0; private float timer = 0f; // Start is called before the first frame update void Start() { spriteRenderer = GetComponent<SpriteRenderer>(); } // Update is called once per frame void Update() { timer += Time.deltaTime; if (timer >= animationSpeed) { timer = 0f; currentSpriteIndex = (currentSpriteIndex + 1) % coinSprites.Length; spriteRenderer.sprite = coinSprites[currentSpriteIndex]; } } }