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.
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:
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° (???)
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 ).
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.
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.