- Home /
How to show a dice for a random number with 2D Animation.
Hi I am new in Unity. I am trying to create e simple dice game. where I have used a 2D dice animation.
void Update () {
if (Input.GetMouseButtonDown (0)) {
if (animation["DiceAnim"].speed != 0){
("DiceAnim");
dNum = Random.Range(1,7);
s = animation["DiceAnim"].speed;
animation["DiceAnim"].speed = 0;
animation["chokkaAnim"].time = dNum;
}
else
animation["DiceAnim"].speed = s;
}
}
I have use this code for start and stop animation but could not find the way how to show the correct key frame for the dice when it stops. Anyone please give a better idea to solve this. Thanks in advance. (sorry for my english)
Answer by Jeff-Kesselman · Jun 21, 2014 at 08:12 PM
You're going about this the wrong way.
Either (a) Have a separate animation that ends on each number
OR
(b) have one animation, as well as all the numbers as individual sprites. when the animation ends, repalce it with the right number sprite.
Thanks for your quick respond. I have individual sprites for all 6 numbers. Can you please show me How can I replace the animation with a sprite? (sorry I am very beginner)
Well you really have two options....
(1) Create a new sprite with the number as ist image, set its transform equal to the animation sprite's transform and hide or dispose of the animation sprite
(2) Create single frame animations for each number and use $$anonymous$$ecanim's state machine to control what is displayed.
Number two is the more sophisticated and cleaner solution. See this tutorial for a good example of how to do such a multi-animation sprite.
http://michaelcum$$anonymous$$gs.net/mathoms/creating-2d-animated-sprites-using-unity-4.3
Your answer
![](https://koobas.hobune.stream/wayback/20220613152214im_/https://answers.unity.com/themes/thub/images/avi.jpg)