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
0
Question by Rhaud · May 03, 2013 at 03:02 PM · cubedice

Determining a cube's face up

Hi,

I'm, trying to put together a small proof of concept of a dice game.

As I'm new to Unity i've already stumbled upon my first snag. I can't find a way to determine a cube's face up, even after googling for about an hour or so.

Here's the code that throws 5 randomly rotated dice on a bouncy plane (to get a somewhat unpredictable result each time):

     using UnityEngine;
     using System.Collections;
     
     public class Spawn : MonoBehaviour {
     
         void Start()
         {
             
             for (int x = 0; x <5; x++)
             {
                     int a = 10;
                     int b = 20;
                     int c = 10;
                     GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                     cube.AddComponent<Rigidbody>();
                     cube.transform.position = new Vector3((a+x), (b+x), (c+x));
                     cube.transform.rotation = Random.rotation;
             }
         }
     }
 


Is there an easy way for determining the cube's face up and printing it to console, after they stopped moving?

Many thanks,

R

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by robertbu · May 03, 2013 at 03:06 PM

The six sides of the cube can be identified as:

  • transform.up

  • -transform.up

  • transform.right

  • -transform.right

  • transform.forward

  • -transform.forward

You can test the angle between each one and Vector3.up:

angle = Vector3.Angle(transform.up, Vector3.up);

The one with the lowest angle will be the side facing up. If you are sure the cube will always be flat (i.e. one won't end up leaning against another), you can stop when you find one below a small threshold.

An alternate calculation is to use Vector3.Dot() between the two vectors. The one closest to 1.0 will be the side facing up.

Comment
Add comment · Show 5 · 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 robertbu · May 03, 2013 at 03:14 PM 0
Share

And you can deter$$anonymous$$e when the dice come to rest by checking Rigidbody.velocity and Rigidbody.angularVelocity. I've not tested it, but you might be able to use Rigidbody.isSleeping as well.

avatar image Rhaud · May 03, 2013 at 04:32 PM 0
Share

Just ran into another problem.

Before deter$$anonymous$$ing which way is up I need to see if they're stopped so I have added a IsSleeping check which is not working:

 using UnityEngine;
 using System.Collections;
 
 public class Spawn : $$anonymous$$onoBehaviour {
     
     public bool mx;
     
     void Start()
     {
         mx = false;
         
         for (int x = 0; x <1; x++)
         {
             int a = 10;
             int b = 20;
             int c = 10;
             GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
             cube.AddComponent<Rigidbody>();
             cube.transform.position = new Vector3((a+x), (b+x), (c+x));
             cube.transform.rotation = Random.rotation;
             
                     
             if (cube.rigidbody.IsSleeping())
             {
                 mx = true;
             }
             
             
         }
         
     }
     
         void Update ()
         {        
             if (mx == true){
                 print ("Sleep tight");
             }
         
         }
             
 }
avatar image ExTheSea · May 03, 2013 at 04:33 PM 0
Share

This clearly should have been a comment. I converted it for you this time but next time please post these things as a comment right away.

  • Read this page : http://answers.unity3d.com/page/newuser.html

  • Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

avatar image robertbu · May 03, 2013 at 06:18 PM 0
Share

I've never used isSleeping for this behavior. Just check that the magnitude of Rigidbody.velocity and Rigidbody.angularVelocity is below some threshold.

avatar image Rhaud · May 06, 2013 at 07:13 AM 0
Share

Thanks, ExTheSea, I'll look into that.

avatar image
0

Answer by Rhaud · May 09, 2013 at 12:49 PM

I finally managed to figure this out, after some fidgeting. I'll post everything here if anyone else has the same predicament as myself.

  1. For each side of the cube, I've created a minuscule object that sits just a fraction of a pixel outside of the cube's plane (0.045) so it is triggered only when the cube sits flat on a side. Set it to Box Collider On, Trigger Off.

  2. I've created two floors: one is the trigger listener and the other needs to be in place for the cube not to fall through the first. (couldn't find a way to have an object with both box collider and trigger on at the same time... n00b)

  3. I've attached the following script to the trigger floor plane:

using UnityEngine; using System.Collections;

public class Collision : MonoBehaviour {

 public string diceval;

 void OnTriggerEnter (Collider myTrigger)
 {
     if (myTrigger.gameObject.name == "Side1trigger")
     {
         diceval = ("6 is up");
     }
     
     if (myTrigger.gameObject.name == "Side2trigger")
     {
         diceval = ("5 is up");
     }
     
     if (myTrigger.gameObject.name == "Side3trigger")
     {
         diceval = ("4 is up");
     }
     
     if (myTrigger.gameObject.name == "Side4trigger")
     {
         diceval = ("3 is up");
     }
     
     if (myTrigger.gameObject.name == "Side5trigger")
     {
         diceval = ("2 is up");
     }
     
     if (myTrigger.gameObject.name == "Side6trigger")
     {
         diceval = ("1 is up");
     }
 }

}

  1. Finally, the script for detecting movement and displaying final dice value, attached to an empty game object in the scene. This also 'grabs' the dice I've created and drops it with a random rotation to create an unpredictable outcome each time. Floor is bouncy with a factor of 0.6

    using UnityEngine; using System.Collections;

    public class Spawn : MonoBehaviour {

      public GameObject dice;
         public string isSleeping = ("");
         public bool mx = false;
         public string finalval;
     
         // Use this for initialization
         void Start ()
         {
             dice = GameObject.Find("SixSidedDice");
             dice.transform.position = new Vector3(0, 10, 0);
             dice.transform.rotation = Random.rotation;
         }
         
         // Update is called once per frame
         void Update ()
         {
             //Check if dice is moving
             if (dice.rigidbody.velocity.x == 0.0 && dice.rigidbody.velocity.y == 0.0 && dice.rigidbody.velocity.z == 0.0)
                                     
                 {
                 isSleeping = ("Sleep tight");    
                 
                 //print the dice outcome only once in Update via bool mx switch
                 if (!mx)
                     {
                         mx = true;
                         print (isSleeping);
                 //Find the Collision script and get the final value of the diceval variable
                 //If this is called in Start, diceval has no value because the cube has just been dropped
                     GameObject theValue = GameObject.Find("Floor");
                     Collision collisions = theValue.GetComponent<Collision>();
                     finalval = collisions.diceval;
                     
                         print (finalval);
                     }
                 }
             }
     }
    
    

It might be crude code and solution, but I have the benefit of a beginner :)

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 MultidimensionalShow · Mar 12, 2016 at 09:02 AM

You should not compare floats against zero - as they will have rounding errors. Use should use the maths Epsilon function when checking the velocity of the rigidbody.

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

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

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to make emissive dice in Unity? 1 Answer

How can I throw an object using accelerometer or tilt? 2 Answers

how to put cube above cube? 1 Answer

Why pixel artifacts on aligned cubes? 1 Answer

How can I stop cubes from showing through each other when overlapped? 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