How to turn a part using only an angle?

Hey everyone, I’m trying to make a slider bar that turns a character model for character creation, but I’m having trouble figuring out how to make it rotate properly. The character is already tilted to face the ground and slightly tilted sideways. For reference,
image

The slider bar already returns a value between -180 and 180 based on how far along it is. I wanna set the part’s Y rotation (I think, I’m not very good at CFrame angles) so that it turns the model based on the slider’s value.
This is what it looks like right now.
https://gyazo.com/7d38a037b0e130a1bad0f063ac0e06d5

What I’m trying to do is have it face the floor when it’s in the center, and away when it’s -180 and 180 respectively, everything in between being a linear progression to those states.
However, I’m not able to figure out how to do it. I’ve tried countless things, all of them seem to not work properly. I’ve watched tutorials, read the documentation and I still don’t get it. Can someone help?

A code snippet or two could go a long way in helping you , but at the moment, the speed at which the model is rotating indicates you didnt convert the degrees into radians before rotating the model.

If that is actually your problem, you can convert between the two with

math.deg(rad)

and

math.rad(deg)

You can use this equation:

Y Rot = ((bar position - (bar span / 2)) / (bar span / 2)) * 180

bar position is the position of the slider along the span of the selection bar
bar span is the size of the bar

You can then set the model’s primary part CFrame:

model:SetPrimaryPartCFrame(CFrame.new(model:GetPrimaryPartCFrame().p) * CFrame.Angles(0, yrot, 0))

Nope, it is.
The only variable to keep in mind in this is the angle variable “angle” which is -180 to 180 based on how far along the bar is.

Also, edit, I think I figured out a way to make it work using lerps right as I was writing that

Correct me if I’m wrong, but I’m fairly sure this will give me the same result as in the gif. I believe it will just add the bar’s rotation on top of the old one making it spin around wildly. What I’m trying to do is to just set it as the bar’s rotation.

Sorry about that, I mixed up my code. This should work:

model:SetPrimaryPartCFrame(CFrame.new(model:GetPrimaryPartCFrame().p) * CFrame.Angles(0, math.rad(yrot), 0))

This creates a CFrame with the same position and 0 rotation every frame and then applies the absolute y rotation. Hope this helps! :slight_smile:

You’d math.rad the yrot, but I’ve already tried this;; the problem is that I can’t figure out how to get the old default rotation so that it’s facing the way that it is in the picture on top of it working like this.

You can’t use the orientation since it changes to compensate for the strange tilt and no longer becomes viable, and I don’t know how to get it (as a radiant or degree) from the cframe

I would hard code the Y rotation offset you want and add it to the Y rotation calculated via the calculation above as trying to re-calculate the Y rotation via ToEulerAngles may give you incorrect rotations.

Do you know how to get an object’s cframe rotation in the XYZ format? The one in orientation’s all jumbled up.

You would have to use CFrame:ToEulerAnglesXYZ. I would advise against using this function if possible though because it recalculates the angles to the best of its ability, but doesn’t always get it completely right.

Yeah, I can’t seem to even be able to hard-code it in cause it keeps giving me different values than the one in orientation.
image

This is what the YXZ format gives, even though the wiki states that it should be accurate. image

Alright, the idea I had worked. I simply lerped based on the percentage of where the bar was located. (completely to the left is -1, completely to the right is 1). I saved the default value then the value where it is rotated 180 degrees. I then simply lerped using the percentage.
https://gyazo.com/29a7d8ccf0ec4583d01ebb4ae0b9f0fe
I’m not gonna mark this as a solution cause I feel like marking myself as a solution and it counting towards the list of solutions on my profile is kind of cheap. never mind

Do it anyways, other people might have an issue in the future and come to this thread eventually. There’s also people who’ll come here thinking this is unresolved.

Fine. I do wish these types of solution weren’t counted, though.