Your source code is now downloading. If does not start download please wait for 10 seconds

Your source code is now downloading. If does not start download please wait for 10 seconds

Objective Main objective of this post is to give you an idea about how to roll a dice in Unity 3D.

Step 1 Introduction

Building a Board-Game but troubled with dice; here is the sample code and illustration for how to roll a dice like real dice and how to identify face value of dice apparent on gaming console.

This problem is divided into two major segments:

How to roll a dice? Identify the face value which is random integer between 1and 6(six faced standard dice).

Step 2 Roll A Dice

2.1 Import Model

Import a standard dice model into unity3D. Adjust transform as shown in figure and add rigid body to it.

2.2 Add Script

Now add snippet to script from where dice is controlled.

Note This snippet enables you to roll dice like you swipe on screen with mouse, if you make appropriate change you can easily translate for touch devices.

if (Input.GetMouseButtonDown (0)) { //initial click to roll a dice initPos = Input.mousePosition; //return x component of dice from screen to view point initXpose = cam.ScreenToViewportPoint (Input.mousePosition).x; } //current position of mouse Vector3 currentPos = Input.mousePosition; //get all position along with mouse pointer movement Vector3 newPos = cam.ScreenToWorldPoint (newVector3(currentPos.x,currentPos.y,Mathf.Clamp(currentPos.y/10,10,50))); //translate from screen to world coordinates newPos = cam.ScreenToWorldPoint (currentPos); if (Input.GetMouseButtonUp (0)) { initPos = cam.ScreenToWorldPoint (initPos); //Method use to roll the dice RollTheDice(newPos); //use identify face value on dice StartCoroutine(GetDiceCount ()); } //Method Roll the Dice void RollTheDice(Vector3 lastPos) { diceObject.rigidbody.AddTorque(Vector3.Cross(lastPos, initPos) * 1000, orceMode.Impulse); lastPos.y += 12; diceObject.rigidbody.AddForce (((lastPos - initPos).normalized) * (Vector3.Distance (lastPos, initPos)) * 25 * duceObject.rigidbody.mass); }

2.3 Methods

This is how RollTheDice method works:

Initially, Torque is added to rotate the dice while rolling a dice. Than Force is added so it will give look and feel like real dice is rolled.

Amount of Torque is calculated using Cross Product of lastPos and initPos to that rotate like real dice and in the direction in which mouse is moved.

and to that rotate like real dice and in the direction in which mouse is moved. Similarly Force is added to throw a dice in direction of mouse.

//Coroutine to get dice count void GetDiceCount() { 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; Debug.Log ("diceCount :" + diceCount); }

Step 3 Identify The Random Face Value On Die

Above snippet explains how to identify the random face value on dice.

This snippet must be included in script which is applied to dice in hierarchy and transforms of should be as shown in figure1.

Dot product is used to find which face is to be considered.

Else if ladder is used to evaluate result of dot product between different faces of dice and Vector3.up and result is compared with 1(means both face of dice and Vector3.up are parallel which is indeed required answer)

I hope you found this blog helpful while Roll Dice in Unity 3D. Let me know if you have any questions or concerns regarding Unity 3D, please put a comment here and we will get back to you ASAP.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Game Development Company in India.