Trying to rotate the player along the Y axis(horizontal rotation) via CFrame.fromEulerAnglesXYZ(0,Y,0) to follow the mouse. For some reason, I can’t seem to figure out a way to convert lookVector into a angle or if this is the correct way to go about doing this.
You want to construct a cframe by providing two positions, this should make a cframe for the head that will look at the mouse position. The first Vector3 is the starting position which is the head in this case, the second Vector3 is the point you want to look at which is the mouse in this case.
local mousePos = mouse.hit.p
local headPosition = head.Position
--Make sure the height of the mouse pos is the same as the head or else the head will look up and down instead of just turning on the Y axis
mousePos = Vector3.new(mousePos.X, headPosition.Y, mousePos.Z)
local newHeadCFrame = CFrame.new(headPosition, mousePos)
OH whoops sorry i forgot that you are moving the head of a character around, for that its a bit more complicated
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
mouse.Move:Connect(function()
local pos = mouse.Hit.p
local char = plr.Character
--Convert the C0 of the neck motor to a global space CFrame so we can use it with the mouse position
local neckGlobalCFrame = char.Head.Neck.Part0.CFrame:ToWorldSpace(char.Head.Neck.C0)
--Make sure the mouse position is at the same height at the head or else the head will rotate up and down as well
pos = Vector3.new(pos.X, neckGlobalCFrame.Y, pos.Z)
--Create a cframe that will make the head look at the mouse position
local neckCFrame = CFrame.new(neckGlobalCFrame.p, pos)
--Convert the cframe back to the local space so it can be used in the Neck joint
neckCFrame = char.Head.Neck.Part0.CFrame:ToObjectSpace(neckCFrame)
char.Head.Neck.C0 = neckCFrame
end)
this should work. Put this in a localscript. This should work for an R15 character
I have a custom rig that’s bound together without the use of Motor6Ds. Instead, it uses WeldContraints. Naming conventions do match R15. Not sure if this makes it any different.
If you are trying to turn the player toward the position of the mouse, getting the lookVector is unnecessary
this just a localscript that turns a part in the center towards the mouse
local mouse = game.Players.LocalPlayer:GetMouse()
local part = Instance.new("Part", workspace)
part.Anchored = true
part.CFrame = CFrame.new(Vector3.new(0, 5, 0))
while wait() do
part.CFrame = CFrame.new(part.CFrame.p, mouse.hit.p)
end
idk what the naming convention in your custom rig is but just change the names around to match it so your setting the cframe of the root part of the rig
If you actually wanted to get an angle for euler angles using the look vector, you could just take the math.atan2 between the X and Z components of the look vector:
local angle = math.atan2(-hrp.CFrame.LookVector.X, -hrp.CFrame.LookVector.Z)
print(math.deg(angle), hrp.Orientation.Y) --> nearly identical