NPC Head rotation is causing NPC rotate uncontrollably

What do you want to achieve?

Hi everyone. I’ve been recently testing out a really fun gimmic in my game I found online which is having an NPC’s head follow the dirtection of the player.
What is the issue?

The issue I am running into is that is that the main body is bugging out and causing the NPC to rotate uncontrollably.

At first I though it might have have been the colissions of the NPC Model but this doesn’t seem the issue.
Here is a video of the situation I have:

As you see it is not a normal Humanoid Rig that might be causing the problem, but I will believe I made the NPC pretty soundly but I will send the Model outliner anyway.

This is also the code I am running:

the target is connected to a function that I am using to find the nearest player. Also I have this function playing in a while wait(0.01) loop.

local MIC = script.Parent
local Humaoid = MIC:WaitForChild("Humanoid")
local Root = MIC:FindFirstChild("HumanoidRootPart")
local Head = MIC:FindFirstChild("CamRotator")

function LookAtPlayer(target)
	local rayParams = RaycastParams.new()
	local rayOrigin = Head.Position
	local rayDirection = (target.Position - Head.Position).Unit * 20
	rayParams.FilterDescendantsInstances = {MIC}
	rayParams.FilterType = Enum.RaycastFilterType.Exclude
	
	local Rotator = MIC.MainBody:FindFirstChild("CamRotator")
	local RotC0 = Rotator.C0
	local facing = Root.CFrame.LookVector
	local targetPos = Vector3.new(target.Position.X, target.Position.Y, target.Position.Z)
	local direction = (targetPos - Root.Position).Unit
	local angle = math.acos(facing:Dot(direction))
	local rayCast = workspace:Raycast(rayOrigin, rayDirection, rayParams)
	local info = TweenInfo.new(.3, Enum.EasingStyle.Linear)
	if rayCast then
		local rayInstance = rayCast.Instance
		if rayInstance:IsDescendantOf(target.Parent) then
			local distance = (target.Position - Root.Position).Magnitude
			if distance < 10 then
				wait(0.3)
				local targetC0 = RotC0 * CFrame.Angles(0, angle, 0)
				local tween = TweenService:Create(Rotator, info, { C0 = targetC0 })
				tween:Play()
			end
		end
	else
		return
	end
end

If anyone has an experience with this stuff or sees any errors then the help would be much appriciated!

3 Likes

If that model still Motor6D joints, the rest of the model will move in accordance. Try disabling the Neck motor6D if it exists

1 Like

I get this a try with the Motor6D that connects to the Head Motor6D, but this makes the head disconnect from the main body or the body full aport. Do you think I need to replace the disabled Motor6D with a weld?

I managed to fix this issue.

I had to scrap this script and use a mixture of AlignOrientation and CFrameLookAt

If anyone wants an example of what I did for their own custom rigs then let me know :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.