Question by
fedaricantonyamal · Dec 25, 2020 at 05:52 AM ·
rigidbodyinheritancerandom.rangeaddtorque
I wanted to roll 2 dices at a single time and needs to get the result. But my first dice is working properly not the second. Sharing my script here.
public class Dice : MonoBehaviour { Rigidbody rb;
bool hasLanded;
bool thrown;
Vector3 initPosition;
public DiceSide[] diceSides;
public int diceValue;
// Start is called before the first frame update
void Start()
{
initPosition = transform.position;
rb = GetComponent<Rigidbody>();
rb.useGravity = false;
}
public void RollDice()
{
Reset();
if(!thrown && !hasLanded)
{
thrown = true;
rb.useGravity = true;
rb.AddTorque(Random.Range(0, 700), Random.Range(0, 700), Random.Range(0, 700));
}
else if(thrown && hasLanded)
{
//reset the dice position
Reset();
}
}
void Reset()
{
transform.position = initPosition;
rb.isKinematic = false;
thrown = false;
hasLanded = false;
rb.useGravity = false;
}
void Update()
{
if(rb.IsSleeping() && !hasLanded && thrown)
{
hasLanded = true;
rb.useGravity = false;
rb.isKinematic = true;
//side value check
SideValueCheck();
}
else if(rb.IsSleeping() && hasLanded && diceValue ==0)//If dice didnt drawn properly it will re-roll
{
//roll the dice again
RollAgain();
}
}
void RollAgain()
{
Reset();
thrown = true;
rb.useGravity = true;
rb.AddTorque(Random.Range(0, 700), Random.Range(0, 700), Random.Range(0, 700));
}
void SideValueCheck()
{
diceValue = 0;
foreach(DiceSide side in diceSides)
{
if(side.OnGround())
{
diceValue = side.sideValue + side.sideValue1;
//send results to GameManager script
GameManager.instance.RollDice(diceValue);
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613022144im_/https://answers.unity.com/themes/thub/images/avi.jpg)