I’m having an issue with the CFrame I use to represent my player’s aim rotating randomly at high and low angles. I made a video with voiceover to illustrate:
As you can see, when I’m aiming very high up at a steep angle, the whole thing rotates. As said in the video, I’d like the green part to stay relatively close to the white bar representing the RightVector unit of the humanoid root part.
I’ve tried:
Constructing the cframe using the root part position and a LookAt vector
Calculating exact angles between the root part cframe and the target vector, then applying them as rotations to a new cframe constructed from root part position
Constructing the cframe using euler angles
I also tried a few other things (and did a ton of research), but everything resulted in the same behaviour depicted in the video.
Any help much appreciated, thanks. Been working at this for hours now.
It’s characteristic of how CFrame.new(origin, target) works. Since you’re really only giving it one of three direction vectors needed for a CFrame (look vector) it has to make an assumption that the up vector is (0, 1, 0) so it can create the third right vector. So in the cases where your look and up vector are becoming more and more equal to each other you get strange twisting behaviors.
The way to fix this is to supply more information to the CFrame creation. There are a few ways of going about doing this but the easiest ways are going to be:
where you would supply your direction vector from root part to mouse hit as vX, your camera’s CFrame.UpVector as Vy, and then skip on vZ. You’ll get a CFrame that you can rotate by a constant to be correctly oriented for your use case.
where you would supply all the components of a CFrame. You could use your camera’s CFrame.UpVector and the direction vector from root part to mouse hit to calculate the missing third vector (V3:Cross(V3)). How to think about CFrames is a good read if you want to get a better idea on visualizing all the components.
That’s interesting, I didn’t think about constructing from matrix then rotating it - I was thinking too much about getting the RightVector without the strange behaviours.
Thanks for the response, I’ll have another crack at it in the morning and update if I continue to have issues.