Why is two out of four dice go further

For the first time I am developing something in 3d and with physics. I am following a tutorial to roll a dice.

When I press the button the dices shoots up in the air and roll.

It works well when I have two dices but when I add #3 and #4 those dices goes far up in higher speed. I do not understand what I am doing wrong here.

The dices looks like this:

The following is the code that I have on the four dices:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DiceScript : MonoBehaviour
{

    static Rigidbody rb;
    public static Vector3 diceVelocity;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    public void RollTheDice ()
    {
        DiceNumberTextScript.diceNumber = 0;
        float dirX = Random.Range(0, 500);
        float dirY = Random.Range(0, 500);
        float dirZ = Random.Range(0, 500);
        transform.position = new Vector3(0, 2, 0);
        transform.rotation = Quaternion.identity;
        rb.AddForce(transform.up * 1000);
        rb.AddTorque(dirX, dirY, dirZ);
    }
}

I would appreciate some help so I understand. I have tried to google etc. but still do not understand.

If you set the damping/angularDamping (on the Rigidbody) and/or bounce/friction properties (PhysicMaterial on the Colliders) different, the physics for each dice would behave differently.

Alternately, if you emplace to dice so they overlap initially, then the physics may “jerk” suddenly trying to push them apart. Make sure their colliders are not overlapping at the start, and then you should get much more consistent results, assuming you have the same damping and bounce/friction properties set for all dice.

2 Likes

Yeah I was thinking initial overlap or contact between the dice is a likely cause as well. Try moving them extremely far apart so there is zero chance of contact and see if the problem disappears. If so then slowly move them back together until they are close enough for your purposes but not so close that they are in contact.

Does the other dice hit them?

For example, don’t do this:

 public void RollTheDice ()
    {
        ...
        transform.position = new Vector3(0, 2, 0);
        ...
    }

As that puts all the dice inside each other on the first frame!

2 Likes

Sorry for late answering but I have been traveling, tx for help. Will check and adjust.