Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by wijesijp · Aug 27, 2015 at 06:08 AM · addforce

How to make sure dice turn to a face?

I am working on a dice game in Unity. The dices are meshes with rigid bodies. 3 dices are thrown in to a box using AddForce.

The problem I am having it due to the sides of the box or due to other dices sometimes the dices do not turn fully to a face. So it is confusing to the player to understand which side has turned up

If someone has done similar game is there a solution for this?

Comment
Add comment · Show 5
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 Vice_Versa · Aug 27, 2015 at 07:56 AM 0
Share

im not sure if this would help or not, but if you increased the mass on the rigidbodies, wouldnt that make them more likely to land on the ground ins$$anonymous$$d of overlap?

avatar image _Yash_ · Aug 27, 2015 at 08:42 AM 0
Share

you can add force when two dice collide with each other. you can find point of collision from OnCollision method. This way two dices will never touch each other.

avatar image wijesijp · Aug 27, 2015 at 08:47 AM 0
Share

Increasing the mass didn't change the behavior.

avatar image wijesijp · Aug 27, 2015 at 09:19 AM 0
Share

adding a force in collision maybe a way.

But the dice collide when they are thrown and it is O$$anonymous$$ as long as they are moving. Only when they stop if I can use this method to turn it to a side...

The dice can't just jump off when they are about to stop also.

avatar image Scribe · Aug 27, 2015 at 09:29 AM 1
Share

Once they have stopped, add a force in a direction away from any collision point with either a dice or a wall, until there no longer exist collisions with anything other than the floor. This will slide the dice away from each other and walls.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Needleski · Aug 27, 2015 at 09:49 AM

Hi,

I recently hit the same problem, I resolved it by giving the die or dice which are not facing up a small kick if their rigid body velocity is 0 and its not possible to determine which side is up. Something like this attached to each die ....

 public class DieController : MonoBehaviour {
      public int value = 0;
      private Rigidbody rigidbody;   //Get this in Start()

      private int DetermineUpSide() {
           //This method will return 1 .. 6 unless the side can not be determined, in which case it returns 0
      }
 
      private void Update() {
           if(rigidbody.velocity == Vector3.zero) {
                value = DetermineUpSide();
                if(value == 0) {
                     rigidbody.AddForce(new Vector3(Random.Range(0.5f, 1.0f), Random.Range(0.5f, 1.0f), Random.Range(0.5f, 1.0f), ForceMode.Impulse);
                }
           }
      }
 }

Its not perfect, but, it works!!

Hope it helps!

Comment
Add comment · Show 4 · 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 wijesijp · Aug 27, 2015 at 10:09 AM 0
Share

This is a good idea.

One question I have is how do you detect the up side (Deter$$anonymous$$eUpSide) ?

What I have done is put 6 points away from each face of the dice. When the dice stop I check which point is below the table. So I don’t get a situation that I can’t deter$$anonymous$$e a upside.

avatar image Needleski · Aug 27, 2015 at 10:27 AM 0
Share

I copied this code from another post, but I lost the link. Note, the values returned depend on how your die is UV mapped. I.e. there are 2 dots on the forward vector side of my die, you may have another UV layout, meaning you should modify the returned values to suit......

 private int Deter$$anonymous$$eSideUp() {
     float upThreshold = 0.99f;     //You can modify this, slightly, if you want to allow slight rotations when the die has stopped
 
     float dotFwd = Vector3.Dot(transform.forward, Vector3.up);
     if (dotFwd >= upThreshold) return 2;
     if (dotFwd <= -(upThreshold)) return 5;
             
     float dotRight = Vector3.Dot(transform.right, Vector3.up);
     if (dotRight >= upThreshold) return 4;
     if (dotRight <= -(upThreshold)) return 3;
             
     float dotUp = Vector3.Dot(transform.up, Vector3.up);
     if (dotUp >= upThreshold) return 6;
     if (dotUp <= -(upThreshold)) return 1;
     
     return 0;
 }
avatar image Needleski · Aug 27, 2015 at 10:35 AM 0
Share

I forgot to mention, I also added a Physics material to each die with a "bouncyness" of 0.8. This gives the die a bit of bounce when they hit each other, walls, etc making it less likely (but not impossible) for them to stop at a strange angle.

avatar image wijesijp · Aug 27, 2015 at 11:02 AM 1
Share

This works great ... I think add a torque may look much better ...

avatar image
0

Answer by seahorsevn · Sep 13, 2017 at 04:26 PM

Here is the link for you http://www.theappguruz.com/blog/roll-a-dice-unity-3d

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

31 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 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 avatar image avatar image avatar image

Related Questions

Moving player to the opposite direction of the mouse position 1 Answer

Mistake in Function with Collision and AddForce 0 Answers

2D Rotate towards GameObject acting odd. 2 Answers

Player Follow Function 2 Answers

Applying random forces to all instantiated gameobjects 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