Head face mouse script causing head to dissapear

As soon as the script runs the head either teleports somewhere random or you’re flung into the sky. Is there a way to fix this?

Here’s my code:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local character = script.Parent
local head = character:WaitForChild("Head")

local player = Players.LocalPlayer
local mouse = player:GetMouse()

RunService:BindToRenderStep("lookAtMouse", Enum.RenderPriority.Camera.Value, function()
	local neckJoint = head:FindFirstChild("Neck")
	
	if not neckJoint then
		return
	end
	
	neckJoint.C0 = CFrame.lookAt(head.Position, mouse.Hit.lookVector)
end)
local relativizeToWorld = neckJoint.Part0.CFrame:Inverse()
neckJoint.C0 = relativizeToWorld*CFrame.lookAt(head.Position, mouse.Hit.Position)

Edit: NVM, here is another technique you can use to maintain C0 Position.

local goalCFrame = CFrame.lookAt(head.Position, mouse.Hit.Position)
local relativizeToWorldCFrame = neckJoint.Part0.CFrame:ToObjectSpace(goalCFrame)
local goalRotationOnly = relativizeToWorldCFrame-relativizeToWorldCFrame.Position
neckJoint.C0 = CFrame.new(neckJoint.C0.Position)*goalRotationOnly

But yeah @4SHN has explained it better the C0 is relative to the Part0 which should be the upper torso so we need to inverse it and make it into object space along with the correct CFrame.lookAt() parameters.

First, you’re misunderstanding Neck. C0 is the ObjectCFrame on the UpperTorso for where to attach the Neck. C1 is the ObjectCFrame on the Head for where to attach Neck. The attachment instance in the UpperTorso, NeckRigAttachment’s, CFrame is Neck.C0. And the attachment instance in the Head, NeckRigAttachment’s, CFrame is Neck.C1.
The head was flinging because you were setting C0 to the Head’s CFrame, which changes the head’s cframe and puts you in an endless loop of changing the head’s cframe.

Since you’re trying to make the head face the mouse and be at the head’s correct position, you’d set Neck.C0 to the UpperTorso’s NeckRigAttachment’s Position, then rotate it.
CFrame.new(NeckRigAttachment.Position) * (the cframe rotation of the humanoidRootPart):Inverse() * (how much you want to rotate the head)
you need to multiply the inverse of the body’s direction so the head will face the mouse regardless of the direction the character is facing

Another problem is that you’re misunderstanding the parameters for CFrame.lookAt(…). The second parameter is the position to look at, not the direction to face. So, you’d use mouse.Hit.Position, not the LookVector.

Some side notes that are also issues, you should use RunService.Stepped:Connect( … ) rather than working with the RenderStep.
Info on the Task Scheduler.
And you should unbind whenever the player respawns.

1 Like
local relativizeToWorldCFrame = neckJoint.Part0.CFrame:ToObjectSpace(goalCFrame)
local goalRotationOnly = relativizeToWorldCFrame-relativizeToWorldCFrame.Position

this ignores the direction the character faces, you’d end up with this

EDIT: mb, I misread it

Uhh sorry but, it’s accounted for in this CFrame during the ToObjectSpace.

local relativizeToWorldCFrame = neckJoint.Part0.CFrame:ToObjectSpace(goalCFrame)
1 Like