Rzut kostką z animacją w Unity

Download “Rzut kostką unitypackage”

DiceThrow.unitypackage – Downloaded 13 times – 42.99 KB
public class DiceThrow : MonoBehaviour
{
    [SerializeField] Sprite[] dices;
    SpriteRenderer spriteRend;
    float delayTime = 0.2f;

    private void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            StartCoroutine(ThrowDice());
        }
    }

    IEnumerator ThrowDice()
    {
        spriteRend = GetComponent<SpriteRenderer>();
        spriteRend.color = Color.white;

        for (int i = 0; i < dices.Length; i++)
        {
            spriteRend.sprite = dices[i];
            yield return new WaitForSeconds(delayTime);
        }

        yield return new WaitForSeconds(delayTime);
        int randomNumber = Random.Range(0, dices.Length);
        spriteRend.color = Color.green;
        spriteRend.sprite = dices[randomNumber];
    }
}

Link do sprite’a kostki

https://pixabay.com/vectors/dice-games-game-six-sided-face-26772/

Scroll to Top