Support for head rotation dependant on Mouse Location

Hello there!
I’m currently working on a game, in which the players camera is fixed in a kind of ‘2D Manner’. I want the player’s head to always be looking at the mouse, I have successfully made the character itself turn in the correct direction, depending on what side of the screen the mouse is located, but when rotating the characters head, using the ‘Neck’ Motor6D, I run into some issues.

Currently this is what I am doing:

local A = Mouse.Y - Mouse.ViewSizeY / 2
local B = Mouse.X - Mouse.ViewSizeX / 2
local Angle = math.deg(math.asin(A/B))

if Mouse.X > Mouse.ViewSizeX / 2 then
	Angle = Angle * -1
end

Client.Character:FindFirstChild('Torso'):FindFirstChild('Neck').C0 = CFrame.new(0, 1, 0) * CFrame.Angles(math.rad(Angle),math.rad(180),0)

But sadly, this happens:
https://gyazo.com/e5ad4db23b981e64c68cf7f481f4607e

I don’t know if this is an issue with my math, or whatelse could be the issue, but I’m clueless.
Any help is appreciated!

I had the same problem (I know its kind of late but i just found out u got the same problem)

I found a way for the perfect head rotation

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local PlayerMouse = Player:GetMouse()

local Camera = workspace.CurrentCamera

local Character = Player.Character or Player.CharacterAdded:Wait()
local Head = Character:WaitForChild("Head")
local Torso = Character:WaitForChild("Torso")
local Neck = Torso:WaitForChild("Neck")

local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local NeckOriginC0 = Neck.C0

Neck.MaxVelocity = 1/2

RunService.RenderStepped:Connect(function() 
	local CameraCFrame = Camera.CoordinateFrame

	if Character:FindFirstChild("Head") then
		local HeadPosition = Head.CFrame.p

		if Neck then
			if Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then
				local Point = Vector3.new(HumanoidRootPart.Position.X,PlayerMouse.Hit.p.Y,PlayerMouse.Hit.p.Z) --PlayerMouse.Hit.p

				local Distance = (Head.CFrame.p - Point).magnitude
				local Difference = Head.CFrame.Y - Point.Y

				Neck.C0 = Neck.C0:lerp(NeckOriginC0 * CFrame.Angles((math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(Torso.CFrame.lookVector)).Y * 1, 0), 1)
			end
		end
	end	
end)

i hope that helps for further projects lol

1 Like

hey do you have any idea how to remove the rotation limit? thanks