Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to get predermined Output of Two Dices rolled randomly in Unity in a realistic way

Discussion in 'Physics' started by chethanv_777, Oct 28, 2022.

  1. chethanv_777

    chethanv_777

    Joined:
    Oct 26, 2022
    Posts:
    6
    I am developing a game in Unity in which if you throw two dices it should roll normally, but you have to get a predetermined output based on the user input. Say user enter 2,5 and presses enter, the dice should fall on the ground and roll in such a way that the first dice results in 2 and second dice results in 5

    To achieve this I have split the rolling logic into two parts. First part is till the dice touches the ground I use the physics and the second part when it starts rolling slowly I use leanTween

    So when I press enter the code is as shown below:

    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Return))
    2.        {
    3.            transform.position = initialPosition;
    4.            transform.rotation = Quaternion.Euler(75f, 180f, 270f);
    5.            initialPush = true;
    6.            collidedWithGround = false;
    7.            rollCompleted = false;
    8.            // above conditions repeated so that in play mode when u enter the numbers again and get the things done, the values are reset          
    9.             rigidBodyCube.useGravity = true;
    10.             MagnitudeX = Random.Range(0f, 1000f);
    11.             MagnitudeY = Random.Range(0f, 1000f);
    12.             MagnitudeZ = Random.Range(0f, 1000f);
    13.        }`

    Code (CSharp):
    1. `private void FixedUpdate()
    2.    {
    3.       if(initialPush)
    4.        {
    5.            rigidBodyCube.AddForce(new Vector3(pushForce, -pushForce,0f));                    
    6.            rigidBodyCube.AddTorque(MagnitudeX, MagnitudeY, MagnitudeZ);          
    7.        }
    8.        if (collidedWithGround && !rollCompleted)
    9.        {
    10.            RollDiceToDesiredNumber();
    11.        }
    12.    }`
    Code (CSharp):
    1. private void OnCollisionEnter(Collision other)
    2. { if (other.gameObject.name == "Ground")
    3. {
    4. //Debug.Log("Dice collided with Ground..........");
    5. initialPush = false; collidedWithGround = true;
    6.  
    7.    }
    8. }
    Code (CSharp):
    1. `private void RollDiceToDesiredNumber()
    2.    {
    3.        int diceNumber = int.Parse(finalResult.GetComponent<TMP_InputField>().text);
    4.        LeanTween.rotateLocal(gameObject, wantedRotation[diceNumber - 1], timeRoll).setOnComplete(() => rollCompleted = true);
    5.    }`
    So to explain the above code,when I press enter I make initialPush true. In the fixed update I add a sideward force on the pair of dice so that it falls from some space above onto the ground.The rigidBodyCube.AddTorque is so that the dices can rotate as it keeps falling

    When it hits the ground, to make sure the forward push is ceased, I make initial push = false. Now I make the collidedWithGround =true. From there on lean tween takes control of stuff till it gets the result to a determined value.

    The value of the wanted rotations which I have given in the start. It looks like this:

    wantedRotation[0] = new Vector3(270f, 0, 0); wantedRotation[1] = new Vector3(0, 0, 0); wantedRotation[2] = new Vector3(0, 0, 270f); wantedRotation[3] = new Vector3(0, 0, 90f); wantedRotation[4] = new Vector3(180f, 0f, 0f); wantedRotation[5] = new Vector3(90f, 0f, 0f);

    It works fine, but not in a realistic way. That is sometimes when it hits the ground, the pair of dices revolve backwards, where it actually had to revolve forward, which gives an awkward look.I tried using the rotation.lerp function but the dices are continously rotating.I tried to search how to rotate the dice slowly using physics till the desired rotation but I am not getting any proper results. I tried to use Lean tween from the moment it was thrown till it hits the ground, but there is this time variable which makes it hard for me to correct predict when it hits the ground correctly. So how to get a realistic solution for this problem?
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    MelvMay likes this.