Camera LookVector to Eular Angle

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.

Any replies are welcome. Thanks in advance.

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)

Doesn’t seem to work on my end.

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

1 Like

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.

welds will work the same, you just need to change

char.Head.Neck

to wherever your Neck joint is in your custom rig

It might be worth noting that I’m trying to rotate the entire character and not just the head.

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
1 Like

I think you could just set the CFrame of the HumanoidRootPart or the Torso then and that will change the whole character

local mousePos = mouse.Hit.p
mousePos = Vector3.new(mousePos.X, char.HumanoidRootPart.Position.Y, mousePos.Z)
char.HumaoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.Position, mousePos)

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

I reworked it to my likings and change a few things but, it seemed to have worked. Thanks so much.

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

The other replies’ methods are better though.

1 Like