Best way to read the face of a die?

I am not sure if this is the place to post this but I have a few issues that I am trying to solve. At the moment, I am just experimenting with a few things but really use some help. To start with, I am trying to get a single dice roll to work. I know it would probably be easier to animate it through Blender or something and import it and then just choose it at random but I have no idea how to do that. So, instead, I modeled the die, put it in a small room surrounded by blocking volumes and added impulse to it when the button is pressed. This works for the most part. It is launched properly and rotates and bounces off the wall and I have it set to return to the original position after 5 seconds with the number it landed on facing up so the roll is always random, however, I can only seem to throw the die in one direction. Nothing I do changes it. While that is not my main issue, anything to fix that would be helpful.

What I am really wondering is how to read the face of the die. I know there are a few ways to do this. I can use trigger boxes on each face to determine what is facing up. The issue with that is, I can’t figure out how to determine if the velocity is zero so, every time it rolls and hits the ground, it is read and that obviously doesn’t work. Probably a simple fix but I don’t know it yet. I know that I can also use a line trace to determine it but I have not used them before. Finally, I think I can use an array and determine which is facing up by the rotation and just output that as a value but again, I am not sure how to do that. Is there a better way of doing this? Is it easier to use an imported animation? Thanks!

This is what I am looking to do minus one die and I don’t need to keep track of the highest score, just read the face and output a value. I can understand some of it but there are some things that I don’t get. I am sure I will be able to work things out for the most part so thank you! However, I was hoping for an explanation rather than an example. I won’t learn anything from just copying things, you know?

You could add 6 scene components as children to your dice mesh, position and rotate them so that they are on the correct face, give them a component tag which is the number of the face “1”, “6”, “3” etc. Then on begin play or in the construction script, add these to an array of scene components.

In mine, I disabled “Start with Tick Enabled” on Dice(Self) in the components list. I also made it so when the player presses Q, it called a custom event called “Throw Dice”, this added an impulse and angular impulse to the cube, then added a delay of 0.05 seconds, then Set Act Tick Enabled to true.

Then on Event Tick, get the dice mesh, Get component Velocity and get a Equal (Vector) node, plug the velocity into the top pin and keep the 2nd pin at 0,0,0. Set the float pin to 0.05 (this is the error tolerance). This will check the velocity of the cube to 0,0,0 if it’s nearly equal to that within a range of 0.05 then it’s true.

Add a branch from this, and from True, do a For Each Loop on the Scene Components Array. Drag off from Array Element and get its forward vector and check if it’s equal to 0.0, 0.0, 1.0 with the error tolerance at default (0.0001). get another branch, if this is true, get Array and “Get (A Copy)”. Plug the Array Index from the ForEachLoop node into this. Then you can Get Component Tags and Get (A Ref) but keep the index at 0. Drag from this and do “To String (Name)” and from this do “String To Int”. This will return the value that the dice shows (The face that is point up towards the sky.

Also, probably best to turn off Actor Tick Enabled after this if you dont need it to constantly update after it’s stopped.


So this ForEachLoop checks every element in the Array and gets its Forward Vector, this is the vector that is the Red Arrow in the viewport, and the vector is the way that the object is facing forward. We want to check that the forward vector is facing 0,0,1 which is up towards the sky. Only 1 scene component will return the vector 0,0,1. So when we have 1 component that is facing Up, we want to get this component from the array so that we’re only dealing with this one. This component has its own unique Component Tag so we want to get all of the component tags that it has, but only get index 0 which is the first in the array. This will return the number as a name, so we need to get this to an int. FNames don’t have To Int conversions but FStrings do, so we have to convert this to a String first then use the String To Int node to convert it to an int.



Hi Faittx,

Are you using the Add Impulse node? You should be able to change the impulse vector to change the direction of the impulse.
You could use the “Random Unit Vector” node and multiply by a float to throw the die in a random direction.

Maybe another approach (don’t know if better, just another thinking about it?):
–> Have the “number components” attached to each side of the dice, but offset them a few units.
–> Test for dice velocity as described above
–> When the dice came to a halt, perform either a line trace up from the dice, or a ComponentOverlapCheck above the dice in order to find the number component that is currently there. You probably have to test which works better for getting the desired component. In that way you would avoid checking all the number components for finding which one is up.

Compare actor vectors to determine which face is facing up. E.q. Up Vector = 1, down vector facing up = 5 and so forth.

1 Like

Like so:

Result:

The Die LUT variable maps the output of the vector math to a roll value. The values on the right column will probably need to be rearranged for your particular setup. Let me know if you’d like an explanation of how it works.

2 Likes

THAT is a very elegant way :slight_smile:

Thanks for this ! Works like a charm :wink:

Hi. Can you explain me how to subtract vector from forward vector and get a float number?

That’s the dot product node.

1 Like