Dice throwing help...

So I followed along with this tutorial on YouTube:

I’ve checked the scripts and settings I don’t know how many times and for some reason I keep getting a 0 has been rolled in the console. My scripts are pretty much the exact same, especially the parts that should detect what number has been rolled.

The major difference is that I made my own dice block in Blender (because I thought it was a pretty simple thing to start with in Blender too)

When I do the Sphere Colliders, they aren’t showing up in the little preview window at the bottom - they will with the mesh renderer checked off of course, but obviously that shouldn’t be checked off. So I’m guessing somewhere in there, the sphere collider/trigger isn’t working.

I did name a few things different (like mine was “floor” instead of ground, I’ve checked this to make sure I did that right)

public class ValueCheck : MonoBehaviour {

    public bool onGround;
    public int sideValue;

    void OnTriggerStay(Collider col)
    {

        if (col.tag == "floor") {
       
            onGround = true;
       
        }

        }

    void OnTriggerExit(Collider col)
    {

        if (col.tag == "floor") {
            onGround = false;
        }

    }

    public bool OnGround()
    {
        return onGround;
    }
}

And then this is added to the dice script:

    void SideValueCheck()
    {
        diceValue = 0;
        foreach (ValueCheck side in diceSides) {

            if (side.OnGround ())
                diceValue = side.sideValue;
            Debug.Log (diceValue + " has been rolled!");
        }

    }

And of course the SideValueCheck was added to Update.

Does anyone know why the trigger isn’t detecting a value and keeps giving me a 0?

Make sure of all the right rigidbody settings ( floor has rigidbody? ) and check the project settings collision matrix. Or just debug log OnTriggerStay to see if it registers anything.
But imho this is not a good way to make dice as you depend on OnTriggerExit being called. I’d just define the 6 rotations the dice can have and when the velocity of the dice is 0 check Quaternion.Angle to the 6 rotations and the one with the smallest angle is correct. And if the smallest angle is higher than a certain amount the dice is cocked.

1 Like

First of all, I wanna say I appreciate your reply!

I have no idea (yet) how to do your suggestion. I’ve mostly only been casually learning things here and there and doing the best I can.

Anyway, so I did do a Debug.Log for both OnTriggerStay and OnTriggerExit and neither one was giving any results. I always tried having a rigidbody on floor, it didn’t do anything. Checked settings, everything seems okay to me.

I even tried uninstalling and reinstalling Unity because I found someone with a very similar issue and that worked for them, but still not for me.

Tried changing OnTriggerStay to “OnTriggerEnter” and it didn’t matter.

The only thing I ever get is “0 has been rolled!” in the console, and it also happens 6 times as if it’s detecting all 6 sides yet giving 0 as a result.

It could be anything. Collider not set to trigger. Wrong collider place. Script on wrong object… We don’t have enough info to help.
Needed a dice script for my game anyway so here’s what i just rolled:

using UnityEngine;

public class Dice : MonoBehaviour {

    private Quaternion upRot;
    private Rigidbody rb;
    private Quaternion[] sides;
    public int currentSide;

    void Start () {

        rb = GetComponent<Rigidbody> ();

        upRot= Quaternion.LookRotation (Vector3.up);

        sides = new Quaternion[6];

    }

    void Update () {

        if (rb.velocity != Vector3.zero) return;

        CheckSide ();
    }
    public void CheckSide () {

        sides[0] = Quaternion.LookRotation (transform.up);
        sides[1] = Quaternion.LookRotation (-transform.up);
        sides[2] = Quaternion.LookRotation (transform.right);
        sides[3] = Quaternion.LookRotation (-transform.right);
        sides[4] = Quaternion.LookRotation (transform.forward);
        sides[5] = Quaternion.LookRotation (-transform.forward);

        currentSide = 0;
        float lowestAngle = 1000;
        for (int i = 0; i < 6; i++) {
            var angle = Quaternion.Angle (sides[i], upRot);
            if (angle < lowestAngle) {
                lowestAngle = angle;
                // +1 because there's no zero on dice
                currentSide = i + 1;
            }
        }
    }
}

Of course you should only call CheckSide once after a roll but that depends on your game then. :wink:
The first 6 lines of CheckSide need to be changed depending on where the numbers are on your dice 3d model.

1 Like

Thank you for your help. Greatly appreciated! It’s finally displaying the result properly :slight_smile:

1 Like