# Rotation not working like intended

1. What do you want to achieve?
I want to rotate my character according to the camera rotation with the following code:
``````local rotation -- the camera rotation for the y axis in degrees (between 0 and 360 with %360)
local rot = rot*math.pi/180 -- passing it to the server and back to all clients in this format (rad)

rootpart.CFrame = CFrame.new(pos.x, pos.y, pos.z, 0, math.sin(0.5 * rot), 0, math.cos(0.5 * rot))
``````

The Value “rot” (for rotation) changes between 0 and 360 depending on the camera orientation. Every time the value gets greater than 360 it sets back to 0.

1. What is the issue?
The code is working fine (orientation of camera and value rot are in sync) and the character rotates like intended between 0 and 360°. But every time the value “rot” gets bigger than 360 (sets back to 0°) the character does a whole 360° rotation back it already turned to start again at 0°. This movement takes like half a second and looks very weird.
Video prove:

2. What solutions have you tried so far?
I already tried to upper the limit from 360 to ~ by removing %360 and it seemed to work at first but every time the value gets bigger than ~720 it automatically snaps back by 180 resulting a problem like the first one maybe because the value for math.rad() can’t get bigger than 720.
The Video for this situation:

I’m out of ideas unfortunately. I’m not sure why it got a rotation animation for the character (maybe so its not so cluncky) so I can actually see it turning from 360° to 0° (???)

2 Likes

are you tweening the cframe? i believe that could be causing the issue

1 Like

no, that’s what confuses me the most

Why not create a rotated CFrame the more typical way instead of from a position and quaternion? The problem could be that your CFrame matrix is getting mangled (ignore my lack of quaternion fluency ).

``````CFrame.new(pos.x, pos.y, pos.z, 0, math.sin(0.5 * rot), 0, math.cos(0.5 * rot))
``````

maybe try

``````CFrame.new(pos.x, pos.y, pos.z) * CFrame.Angles(0, rot, 0)
``````

The tweening looking thing is probably from the physics interpolation. It shouldn’t do this though, so I’m stumped too.

Is the CFrame being set on the server, a single client, or on each client? Also, what view is the video from (server, client, other client)?

1 Like

So I tried two different approaches:

``````CFrame.new(pos.x, pos.y, pos.z) * CFrame.Angles(0, rot, 0)
``````

and

``````CFrame.new(pos.x, pos.y, pos.z) * CFrame.Angles(0, math.rad(rot/math.pi*180), 0)
``````

Same results as before unfortunately.
The CFrame is set on the Client side therefore I needed a second player for the recording (the view of the video) (for every player separately after sending it to the server from every client individually). But that has no influence on the value of the rotation. I checked it by printing the value in the output. Apparently the problem is with the physics interpolation thinking the fastest way possible from 360° to 0° is by going backwards whats complete nonsense to me.

1 Like

Thanks for all your replies but I actually got it after thinking some time about it.
As simply as is sounds the solution to the problem is to use two values for the camera rotation. One that works for the camera (if it uses math.deg(x), x > 720; can also use radians but I’m to lazy to change it) and the second which counts the total degrees added or removed from the script that NEEDS to be recalculated to radians in order to function (the rotation of the character is only working if you use radians because of this damn physics interpolation and radians can go up to ~ in the roblox math functions). Finally its possible to use any code example given in the article (commenters or me) to set the roots orientation the right way.

BRUH

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.