Unity3d Create dice roll with predetermined result

2019-02-26 08:39发布

问题:

I have a prefab that is a die (dice). As my game is multiplayer, I generate the result of the die roll before the player actually rolls. I then send that value to the game. I want to take that value and create a die roll with the result being the predetermined value. What is the best way to go about this? I thought about doing this as an animation (1 for each result) but I have no idea where to start. I've looked for tutorials on creating dice animations but can't find anything. Or, can this be done in code? If so, any pointers on directions would be great.

thanks

回答1:

I've done something similar but it is tricky and a bit complicated. Here are the steps I used to accomplish this.

1.First, position the dice on the place you want it to be with the face you want to be the result to face up(+y-axis).

2.Move it backwards to the position that you want to roll the dice from. While moving it, add a little bit of rotation to it to make it look realistic.

3.Now, while moving it, write another script that records/stores the position/rotation + time difference of the dice in a List. The position/rotation and the time difference can put put in a simple struct then saved in a List.

4.Finally, when player wants to throw the dice, simply write a play function that plays the position from those Lists by assigning the positions/rotation from the List and switching to the next index based on the time difference in the List.

Tips:

A.Do this when Game is loading and do it for each side of the dice and store the List in an array. You can then use random number to select the dice side and play the transform stored.

B.When doing #1 and #2, you can move disable the MeshRenderer so that Player cannot see what's going on.

C.Do this with a RigidBody so that it will look realistic especially, when throwing the dice on a surface. It gets complicated with RigidBody but can be done.

D.You can replace #3 and #4 with animation by creating animation from List of position/rotation from #1 and #2 during run-time.