• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Scott 1 · Dec 02, 2009 at 12:47 AM · physicsrandomdice

Dice Animation and Face Determination

I want to roll a few dice models and be able to determine the face that is UP at the end of the roll on the various dice and use those face values in both JavaScript and/or C# scripts. First, how would you roll dice models in Unity and secondly, can someone give an example of determining which face is up on a die model in JavaScript and C#?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThatsAMorais · Dec 10, 2013 at 04:50 AM 0
Share

Could you mark an answer? You've got 2 pages of them.

avatar image Santosh Patil · Dec 10, 2013 at 10:03 AM 0
Share

private var ismoving : boolean = false; private var startY : float = 0; var cubeSpeed : float; var cubeSize: int;

function Update () {

  if (Input.Get$$anonymous$$eyDown("up") && ismoving == false)  
 {  
     ismoving = true;  
     transform.Find("targetpoint").Translate(0, -cubeSize/2 , cubeSize/2);  
     StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.right, 90.0f,cubeSpeed));  
 }  
 if (Input.Get$$anonymous$$eyDown("down") && ismoving == false)  
 {  
     ismoving = true;  
     transform.Find("targetpoint").Translate(0, -cubeSize/2, -cubeSize/2);  
     StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.right, 90.0f,cubeSpeed));  
 }  
 if (Input.Get$$anonymous$$eyDown("left") && ismoving == false)  
 {  
     ismoving = true;  
     transform.Find("targetpoint").Translate(-cubeSize/2, -cubeSize/2, 0);  
     StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.forward, 90.0,cubeSpeed));  
 }  
 if (Input.Get$$anonymous$$eyDown("right") && ismoving == false)  
 {  
     ismoving = true;  
     transform.Find("targetpoint").Translate(cubeSize/2, -cubeSize/2, 0);  
     StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.forward, 90.0f,cubeSpeed));    
 }

}

function DoRoll (aPoint, aAxis, aAngle, aDuration) {

var tSteps = $$anonymous$$athf.Ceil(aDuration * 30.0); var tAngle = aAngle / tSteps; var pos : Vector3; // declare variable to fix the y position

// Rotate the cube by the point, axis and angle for (var i = 1; i <= tSteps; i++) { transform.RotateAround (aPoint, aAxis, tAngle); yield WaitForSeconds(0.033333); }

// move the targetpoint to the center of the cube transform.Find("targetpoint").position = transform.position;

// $$anonymous$$ake sure the y position is correct pos = transform.position; pos.y = startY; transform.position = pos;

// $$anonymous$$ake sure the angles are snaping to 90 degrees.
var vec = transform.eulerAngles; vec.x = $$anonymous$$athf.Round(vec.x / 90) 90; vec.y = $$anonymous$$athf.Round(vec.y / 90) 90; vec.z = $$anonymous$$athf.Round(vec.z / 90) * 90; transform.eulerAngles = vec;

// The cube is stoped ismoving = false;
}

avatar image Santosh Patil · Dec 10, 2013 at 10:04 AM 0
Share

you can use above code to roll a cube.

7 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by ThatsAMorais · Nov 07, 2013 at 03:56 AM

My best answer for the question of detecting the value of physics-based moving dice with no predetermined value, something I have used, is to put colliders on the faces (as triggers), and detect the collision with the surface upon which you are rolling by labeling or tagging that surface, and checking OnTriggerEnter in a script for that surface. While that gives you a side that IS NOT the value of the roll, it tells you what is facing up. My way entails the following:

  • Put sphere colliders on the center of each face, as triggers, parented to the die itself

  • Encode them in some way with the value that the dice would have if this side was down against the rolling surface. I put it in the name because I started simple with a d4, but a d6 would have to be named for its opposite face.

  • In OnTriggerEnter(), send a message upwards including the face's value that was triggered.

  • Add a function to a script on the die that receives this message, which takes the face's values (or however you are encoding it, and to whatever entity you are delegating the knowledge about what to imply from the trigger event)

  • This function should set the die's "currentValue"

  • The die's script could also check whether the rigidbody is sleeping, and report that value, by sending a message upwards to a parent object.

The leg work is a little tedious, but the solution is as accurate as you are at labeling them. It avoids using math, as well, and having to figure out that math for a set of Role playing dice.

Sorry I don't have code, nor do i have a solution for the other question.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Azrapse · Nov 07, 2013 at 06:18 AM

You have good answers already, but I will suggest something totally different. Who knows, maybe it gives you some new ideas.

Why don't you do the other way?

  • Determine the results of each die using Random.

  • Place the dice with their final results up on a staging area away from the camera.

  • Apply random forces to their rigidbodies so that they roll around.

  • Record their movement for a couple of seconds (120 positions and rotations each).

  • Finally, play the recorded movement of the dice backwards to the player's camera. It will look like the dice are rolling towards the final positions that you determined with Random at the beginning.

This will let you 'cheat' somehow. For example, having blessed or cursed dice that have guaranteed results >3 or <4 or that only roll 1, or whatever. :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • ‹
  • 1
  • 2

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Im having a Problem with Basic Collision. 1 Answer

How do I break apart an object? 2 Answers

Finding the axis of a curving pipe 0 Answers

Manual collision check 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges