Problem with raycast and position not being smooth

Hello Devs,
I have this Mob that is suppose to look at the player but when the mob looks at the player it does not look at the player smoothly any help here is the video footage. Plus I need help to get the position of the Torso while the player is moving because the player needs to jump for it to detect.

here is the script

local Head = script.Parent

local LookWait = 0
local Distance = 100

while wait(LookWait) do

    local target = nil
    for i,v in pairs(game.Workspace:GetChildren()) do
        local Human = v:FindFirstChild("Humanoid")
        local Torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
        if Human and Torso and Human.Health > 0 then
            if (Torso.Position - Head.Position).magnitude < Distance then
                local HeadRay = Ray.new(Head.Position, (Torso.Position - Head.Position).Unit * Distance)
                local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(HeadRay, {Head})
                if hit == Torso then
                    target = Torso
                end
            end
        end
    end
    if target then
        local Torso = target

        Head.CFrame = CFrame.new(Head.Position, Torso.Position)
    end
end

Ignore the health I am going to use it so the mob can deal damage.
Any help is appreciated.

Your doing this server sided? if so consider doing it client sided.

1 Like

mind I ask how do I do that actually because I tried a local script in the Head and its not working

That’s happening because the raycast might not be always finding the player to look at, I recommend using tweening because without tween it’ll snap the head to the current position of the player, with tweening it’ll tween there thus looking somewhat ‘smoother’ than snapping.

I recommend not looping ray/search once the ‘target’ is found, what you can do is after finding an target and making sure that target is still in-sight never look for another target and instead focus on that, else once the target isn’t “Seen” anymore you can make it search again.

PS: What I recommended was probably not within your scope which is getting the nearest target so just ignore it.

1 Like

The raycast and position are smooth, do you mean that the head rotation is too much ?

Edit: The code is badly written I’d recommended optimizing

1 Like

ClientSide wont work he probably wants the head to look at the nearest player (or atleast from what I can see on his code)

1 Like

I said that because of the network owner was probably the issue, since moving parts in the client side is smoother that on the server.


However, I would recommend doing this with tweenservice, if you want it to look smooth. I left you here the post.

1 Like

I used the tween script down below but i want the mob to look down to the player
here is the footage

ÂżCan you provide the new version of the script?

1 Like
local ts = game:GetService("TweenService")
local obj = script.Parent

game.Players.PlayerAdded:Connect(function(plr)
    wait(3)
    while wait(.1) do
        ts:Create(obj, TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {CFrame = CFrame.new(script.Parent.Position, Vector3.new(plr.Character.Torso.Position.X, obj.Position.Y, plr.Character.Torso.Position.Z))}):Play()
    end
end)

Try this instead!

local ts = game:GetService("TweenService")
local obj = script.Parent

local twi = TweenInfo.new(
	.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In,
	0,
	false,
	0
)

game.Players.PlayerAdded:Connect(function(plr)
	task.wait(3)
	while true do task.wait(.1)

		local lat = {CFrame = CFrame.lookAt(obj.Position, plr.Character.HumanoidRootPart.Position)}
		ts:Create(obj, twi, lat):Play()

	end

end)
1 Like

This is pretty bad since your looping each time a player join so if 10 players joins that’s a 10 loops

1 Like

Thats why he should do it client sided since is better for this things.

1 Like

Not true, he needs the mob to look at the player and damage him it’d look weird on the client

1 Like

Use this:

local Head = script.Parent
local Distance = 100
game:GetService("RunService").Stepped:Connect(function()

local target = nil
for i,v in pairs(game:GetService("Players"):GetChildren()) do
	local Char = v.Character
	if Char and Char:FindFirstChild("Head") and Char:FindFirstChild("Humanoid") and Char:FindFirstChild("Humanoid").Health > 0 then
		if (Char.PrimaryPart.Position-Head.Position).Magnitude < Distance then
			local LookVec = Head.CFrame.LookVector.Unit
			local Pos = Char:FindFirstChild("Head").Position
			local Pos2 = Head.Position
			local Pos3 = (Pos-Pos2).Unit
			local DotProduct = LookVec:Dot(Pos3)
			if DotProduct > 0.5 then
				target = Char.HumanoidRootPart
			end				
		end
	end
end
if target then
	local Torso = target

	Head.CFrame = CFrame.new(Head.Position, Torso.Position)
end
end)
3 Likes

Well, using a mix of both seems to be the best way, moving it from the client and replicating it to all clients.

1 Like

thanks for the help
i dont know yet if it will get the closest player within its range but i will try it later

I didn’t get the closest player but you can change the script

1 Like