Hello there, I have a problem with some dices to throw on a board and I’m not able to find an answer into the forums. I have been searching this for 3 days and I got nothing, so I decided to post the question here.
I’m creating a board game in C# and, of course, I have to do some dice rollings. I have a button that makes the dice roll when I press it and I have another function to set them to the original position to do another throw, but I have a problem at the time to get the values of the sides. I don’t know how to see when the dice are stopped to get the face values.
I got a script that gets the values but it gets all the values of the sides that collide with the board, not only the final side, when the die has stopped moving.
Here’s the die script:
using UnityEngine;
using System.Collections;
public class DieForce : MonoBehaviour
{
public Rigidbody Body;
Vector3 Traslation;
Quaternion Rotation;
public DieForce(Rigidbody rb)
{
Body = rb;
Traslation = rb.transform.position;
Rotation = rb.transform.rotation;
}
void Update()
{ }
public void roll()
{
UnityEngine.Vector3 force = Vector3.zero + new Vector3(9 + 6 * Random.value, .6F + 7 * Random.value, -10 - 4 * Random.value);
Body.AddRelativeForce(force * 25);
}
public void returnToPosition()
{
Body.transform.position = Traslation;
Body.transform.rotation = Rotation;
}
public string getName()
{
return Body.name;
}
}
The fact is that I have to know when the dice stop moving to get the values and continue the game (it’s a Risk-type game).
I’ve tried
if(rigidbody.velocity.magnitude == 0) {//do stuff}
but it never gets value 0, even if the die stops moving.
Another thing is that I have tried to use the coroutines to wait for the dice stop to continue the game and I’m not capable to do this.
My questions are:
- How do I do to get when the dice stop?
- How do I stop the game to wait for the die values?
I’ve found solutions to my problems in Javascript but I need them in C#. I will be very grateful for anyone who can help me. Thank you, I hope I’ve explained me enough.