Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
8

Answer by Jaap Kreijkamp · Dec 02, 2009 at 12:57 AM

A quick and dirty solution would be to calculate the Vector3.Dot product of the x, y, z axises of your cube against Vector3.up. If a value is (close to) 1 that side is up, if a value is (close to) -1, that side is down (so opposite side is up).

Enough info to build your own code with or do you need a script?

EDIT What the heck, here's a script (the side numbers need to be adjusted to how you've got your dice set up, for me, top = 6, bottom = 1, right side = 4, left side = 3, front side = 5, back side = 2.

The code is in C# but shouldn't be hard to convert to Javascript.

using UnityEngine; using System.Collections;

public class JDiceSideUp : MonoBehaviour {

 int CalcSideUp() {
     float dotFwd = Vector3.Dot(transform.forward, Vector3.up);
     if (dotFwd &gt; 0.99f) return 5;
     if (dotFwd &lt; -0.99f) return 2;

     float dotRight = Vector3.Dot(transform.right, Vector3.up);
     if (dotRight &gt; 0.99f) return 4;
     if (dotRight &lt; -0.99f) return 3;

     float dotUp = Vector3.Dot(transform.up, Vector3.up);
     if (dotUp &gt; 0.99f) return 6;
     if (dotUp &lt; -0.99f) return 1;

     return 0;
 }

 void Update() {
     int side = CalcSideUp();
     if (side &gt; 0) Debug.Log("Side = " + side);
 }

}

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 Ricardo · Dec 02, 2009 at 02:33 AM

How you would roll dice would depend on if you're using a physics-based model, determining the result because of the dice bouncing around, or if you want to simulate the rolling to land on a specific number.

If you're going for the physics model, you could do worse than to check out the source code for Rock'n'Roll dice, which I released under the Artistic License about 11 months ago. You can see a video of it here.

Comment
Add comment · Show 1 · 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 ina · Dec 16, 2011 at 06:22 AM 0
Share

the link is down!

avatar image
0

Answer by h2hjastermereel · Dec 03, 2009 at 03:26 PM

Note that in the above code there is dead zone of 0.99. However unlikely if those values come up the dice value will be 0, which is likely not what you want, change some of the operators to be <= or >= etc..

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 Avtcoco · Mar 11, 2011 at 10:44 PM

Working on a prototype which involves dice, I was having the same problem of getting the 'up' face of the rolled dice. I used the 'dot product' solution, which works pretty well.

I'm now facing the extension of the problem: how to determinate the result of a die with n-faces? eg: D4 (with the resulting face at the bottom), D8, D10, etc.

The configuration of each die is different, and I wonder if another approach would work. Some hints maybe:

  • using different materials for each face?
  • using properties on (mesh) faces?
  • using a top-bottom raycast to the center of the die to get the top-most face. But might not work for non-symetric dice?

Any suggestions?

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 marjan · May 31, 2013 at 12:31 PM

late answer, but you can also add 6 empty gameobjects, parented under your model. adjust their positions to the middle of each face. After Rolling you check which one has the highest y position in world space. This works also with rolling objects exept a pyramid. You could store them in an array at special indexes, this it is easy to find an according up value. (any number from 0 to 5 for example.)

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.

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

When dragging a Door open using RIGIDBODYSCRIPT Plays sound. 1 Answer

Determine Area using code 1 Answer

How to keep physics consistent? 1 Answer

constant force 1 Answer


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