1, Introduction 2, Roll a dice 3, import Model 4, Add script 5, Method 6, identify the random face value on the dice objective This article is intended to give you an idea of how to roll a dice in Unity 3D. The first step is to introduce 1, build a board game, but there is a problem with the dice; here is a sample code that shows how to roll the dice like a real dice and how to recognize the surface values of the dice on the game Control 2. 3, this question is divided into two main parts: 4, how to roll the dice? 5. Determine the face value of a random integer (1 standard dice) between 6 and 6. Step two Dice 2.1 The import model imports a standard dice model into Unity3d. Adjusts the transformation, and adds a rigid body to it. 2.2 Adding a script now adds the code snippet to the script. Note that this code allows you to roll the dice, just as you swipe the mouse over the screen, and if you make the appropriate changes, you can easily convert the touch device.
if(Input.getmousebuttondown (0)){//Initial Click to roll a diceInitpos =input.mouseposition;//return x component of dice from screens to view pointInitxpose =Cam. Screentoviewportpoint (input.mouseposition). x; //Current position of mouseVector3 Currentpos =input.mouseposition;//get all position along with mouse pointer movementVector3 Newpos = Cam. Screentoworldpoint (NewVector3 (Currentpos.x,currentpos.y,mathf.clamp (currentpos.y/Ten,Ten, -))); //translate from screens to world coordinatesNewpos =Cam. Screentoworldpoint (Currentpos); if(Input.getmousebuttonup (0) ) {Initpos=Cam. Screentoworldpoint (Initpos); //Method Use-roll the diceRollthedice (newpos);//Use identify face value on diceStartcoroutine (Getdicecount ());} //Method Roll the DicevoidRollthedice (Vector3 lastpos) {diceObject.rigidbody.AddTorque (Vector3.cross (Lastpos), Initpos)* +, Orcemode.impulse); Lastpos.y+= A;d IceObject.rigidbody.AddForce ((Lastpos-Initpos). Normalized) * (Vector3.distance (Lastpos, Initpos)) * -*duceObject.rigidbody.mass);}
2.3 Methods
This is how the dice-throwing method works:
Initially, when you roll the dice, increase the torque to rotate the dice. It looks and feels just like the real dice roll.
The amount of torque is calculated using the cross product of Lastpos and Initpos, just like the real dice and the direction of the mouse movement.
The same force is added to the mouse to throw the dice in the direction.
//coroutine to get dice countvoidGetdicecount () {if(Vector3.dot (Transform.forward, Vector3.up) >1) Dicecount=5;if(Vector3.dot (-transform.forward, Vector3.up) >1) Dicecount=2;if(Vector3.dot (Transform.up, Vector3.up) >1) Dicecount=3;if(Vector3.dot (-transform.up, Vector3.up) >1) Dicecount=4;if(Vector3.dot (Transform.right, Vector3.up) >1) Dicecount=6;if(Vector3.dot (-transform.right, Vector3.up) >1) Dicecount=1;D Ebug. Log ("Dicecount:"+dicecount);}
The third step is to determine the random value of the dice the above code snippet explains how to identify the random face value on the dice. This code fragment must be included in the script, which is applied to the dice in the hierarchy, and the conversion should be shown in 1. The dot product is used to find out which side to consider. Also, if the ladder is used to evaluate the results of the dot product between different dice and vectors. The upward and the result are compared to 1 (meaning dice and vector 3. Up is parallel, this is really the answer needed) I hope you find this blog helpful when you are in Unity 3D. If you have any questions or concerns about Unity 3D, please leave a comment here and we will get back to you as soon as possible.
Original link: http://www.theappguruz.com/blog/roll-a-dice-unity-3d
How to roll a dice in Unity 3D