- Home /
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?
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?
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.
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.
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.
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!
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.
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;
}
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.
This works great ... I think add a torque may look much better ...
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
Your answer
