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!