Reading the face of Dice?

Hello! Friendly noob here! I have a dice in game you can role, but how would I make a BP to read what side is up and pass the value? Well I know how to pass value, but setting values to different side and such is a mystery to me. Any body know where I can start with this?

Few different approaches. First, how are your dice textured? Separate texture per face or one texture wrapped?

The easiest way I can think of off the top of my head is to have a box component for each face. When something collides with the component you set a value to what would show on the opposite side. Making sure that only the surface the die rolls on can trigger the components is important though. :slight_smile: This means when the die stops moving one only one of the box components should be colliding with the floor. Hope this helps.

My die is 8 sided diamond shaped and it has one texture, but I can always add more. How would you use that method? With the box component, how would you use box because when you roll it, it will collide with the surface when its rolling. Then how would you get up side when it stops?

Set up a trace for each face and then get trace that hits the floor when velocity is 0 - the opposite side of the hit trace is the face up. Since you already know what number is which trace’s opposite face it shouldnt be difficult. Not practical for more faces though, i’m afraid. :\

Here’s a picture that might help you. The box component would set a variable to a value of that on the opposite side. As the die rolls it’ll keep change the variable until it finally comes to rest.

I will try these methods. What about texture that jared was talking about? Can you use elements?

I’d look at the orientation of the die when it came to rest instead of trying to use box component sensors or something.

Something along the lines of converting the rotation into a value and comparing that against each of a set of ‘ideal orientations’ in an array. This way you can make a single parent blueprint for all dies with the logic in there, and in subclasses you just have to change the contents of the array and the static mesh.

How to convert the rotation into that value / what to store in the array is the only tricky bit. I’d have to play around to be sure but probably use GetUpVector on the actor rotation. You’d compare the length of the result of subtracting that from each element in the array, and for a D6 you’d have 100, 010, 001, etc… in the array. The face that is ‘up’ is the one that has the smallest distance to the ideal up vector.

Probably also worth having a timer, so if the die is still moving after X time you could call a ‘reroll’. Ditto if the distance to any of the ‘ideal orientations’ is too great, which would mean that it landed in a crack or something, making it up on an edge or vertex.

Cheers,
Michael Noland

What about other methods, say 2D. If I wanted to make a HUD with dice faces on screen, that change similar to a slot machine. Randomly change faces and values set to a timer, then when stoped reads values. Can you switch pics and values with UMG like that? Just exploring my options if I can get it to work the way I want.

My method would have involved getting the die position in the world when you want to check the value and sending a line trace down from the sky directly above it. The line trace would return the material from which you could get the texture and if the texture is this texture, then the face up is this value. That works if you have separate textures per face. If you’re using a single texture, you could return the hit normal and say if the hit normal is within this vector range then it’s this value.

Of course, there are so many different ways to handle this and others in this thread have thrown superb ideas! even your 2D UMG implementation would work. It’s absolutely possible to switch out UMG textures at runtime, so if that’s an easier way to do it for you, it’s 100% possible.

create sockets on each face and compare the z values of there world Position :slight_smile: