Hey guys,
So I was working on a head movement script that rotates the Neck Motor6D of the player's character based on the Camera LookVector. The script I developed works, and uses the Cross Product of the Camera LookVector and the HumanoidRootPart LookVector.
The y component of the Cross Product is equal to the Sine of the angle between the two vectors, meaning I can obtain the angle that I am looking at. This only works because both LookVectors are Unit Vectors, meaning they have a magnitude of 1.
Since the cross product is the sine of the angle between the Camera LookVector and the HumanoidRootPart LookVector, all I have to do is get the inverse sine of the cross product's Y component to get the angle needed to rotate in the XZ plane.
The problem with this however, is that when the character's head is rotated 90 degrees in the yaw and you try rotating pitch, the head starts rolling rather than looking up and down. You can see this here:
https://gyazo.com/7bc8a834ebfe1fd0c6fc8e568a4b2a48
My question is how I can fix this so that the player's head rotates up and down how it should rather than rolling.
Here is my Head Rotation script:
local RunService = game:GetService("RunService") local Player = game.Players.LocalPlayer repeat wait() until Player.Character local Character = Player.Character local Mouse = Player:GetMouse() local Camera = workspace.CurrentCamera local HumanoidRootPart = Character.HumanoidRootPart local Head = Character.Head local Neck = Head:WaitForChild("Neck") local NeckOrigin = Neck.C0 RunService.RenderStepped:connect(function() local LookVector = Camera.CoordinateFrame.LookVector local NormalVector = HumanoidRootPart.CFrame.LookVector local YawAngle = math.asin(NormalVector:Cross(LookVector).Y) local PitchAngle = math.asin(LookVector.Y) Neck.C0 = NeckOrigin*CFrame.Angles(PitchAngle, YawAngle, 0) end)
I hope someone can answer this question, it would really help me a lot. I've been stuck on this for a while, and I haven't had a linear algebra class I'm still in highschool lmao