How to see which way a part is facing

I am attempting to make a dice roll in a game. In order to make a dice roll game, I must have a way to tell which way the object is facing from a script. However, I have not found anyways to do this. Things I have tried:

Looking it up on the dev hub: I could not find much relevant.
LookVector for CFrame: Replied with a long decimal string (as expected)
Orientation if statements: Apparently orientation doesn’t have just one orientation value for each face facing up

Any help is appreciated,
WolfTamer894

4 Likes

Just insert a texture inside the part

1 Like

I need to be able to detect the orientation from a script (sorry for not mentioning this:)

https://developer.roblox.com/en-us/api-reference/class/Part#topsurface
https://developer.roblox.com/en-us/api-reference/class/Part#rightsurface
https://developer.roblox.com/en-us/api-reference/class/Part#leftsurface
https://developer.roblox.com/en-us/api-reference/class/Part#frontsurface
https://developer.roblox.com/en-us/api-reference/class/Part#bottomsurface
https://developer.roblox.com/en-us/api-reference/class/Part#backsurface

These will get the 6 faces of any part instance.

I do not need to know surface types, rather the direction the part is facing. :slight_smile:

From my understanding, you want to be able to tell which face is pointing upward. You were actually on the right track with LookVector.

Though you shouldn’t only use LookVector. You can also use CFrame.RightVector and CFrame.UpVector. Since you want to know which face is facing upwards, you’ll want to look at the Y value of the Vector3. That is, when you print one of these vectors, it’ll output three coordinate values (X,Y,Z). the Y value will either be 1 or extremely close to 1 in magnitude whenever that particular face is pointing upward. You can access the Y value of a Vector3 easily:

CFrame.LookVector.Y

A standard die has 6 faces. You have three values to go by: LookVector.Y, RightVector.Y, and UpVector.Y. However, the Y value ranges from -1 to 1. Its 1 when the face is pointing directly upward, -1 when the face is pointing directly downward, and 0 when the face is pointing perpendicular to the Y-axis (so sideways).

This means when UpVector.Y is equal to or extremely close to 1 (sometimes you’ll get values like 0.99999 instead of exactly 1), the die’s top surface is on the top. When the bottom surface is on the top instead, UpVector.Y will be -1. This is because whenever the bottom surface is facing upward, the top surface must be facing downward.

You can use this logic with all three values: UpVector.Y, RightVector.Y and LookVector.Y. Every time you compare these three values when the die is on a flat surface, one of them will always be 1 in magnitude (meaning it will either be 1 or -1). Once you determine which Vector3 value has a Y magnitude of 1, you can combine that information with whether or not the Y value is negative to find which surface is facing upwards.

My apologies if this explanation is complex. The solution isn’t too difficult to understand, but it’s a bit difficult to describe. If you need further explanation then I can try to clarify some things.

4 Likes

Have you tried using lookvector?

Thank you! If you want to know how I did it, here it is: I first rolled the dice many times (as this was already a script) and documented the results. I found a pattern where there would only be one number that is not zero (in terms of lookvector, upvector and rightvector). Once I documented these I slapped them into an if statement and it worked!

Thanks again,

WolfTamer894

2 Likes