I want to make the zombie lock at a player but CFrame.lookAt makes it jittery

I’m trying to make the zombie look towards a player, but it ends up being choppy and it makes the zombie stay in midair longer for some reason.

Here is the part:

local Future = targetplr.HumanoidRootPart.CFrame + (targetplr.HumanoidRootPart.Velocity * Epitaph + Vector3.new(0, .1, 0))
Character.PrimaryPart.CFrame = CFrame.lookAt(Character.PrimaryPart.CFrame.Position, Vector3.new(Future.Position.X, Character.PrimaryPart.CFrame.Position.Y, Future.Position.Z))

My question: Is there a way to make it do smoother turns when facing a player and at the same time, not having something that moves itself (CFrame.lookAt makes the zombie jitter when moving)? Is there an alternative? (I’m trying to make the zombie lock at the nearest player.

3 Likes

You can modify your zombies to use ControllerManagers, which gives a FacingDirection property, but you can also use an AlignPosition and rotate the Attachment linked to it, the official Rthro Drooling Zombie does this.

1 Like

You can try this script:

local npc = script.Parent
local playerService = game:GetService(“Players”)

local detectionRadius = 20
local followSpeed = 10

function findNearestPlayer()
local nearestPlayer = nil
local nearestDistance = detectionRadius

for _, player in pairs(playerService:GetPlayers()) do
    local distance = (npc.Position - player.Character.HumanoidRootPart.Position).Magnitude
    if distance < nearestDistance then
        nearestPlayer = player
        nearestDistance = distance
    end
end

return nearestPlayer

end

function onHeartbeat()
local targetPlayer = findNearestPlayer()

if targetPlayer then
    local direction = (targetPlayer.Character.HumanoidRootPart.Position - npc.Position).unit
    npc:MoveTo(npc.Position + direction * followSpeed * (1 / 60))
end

end

npc:WaitForChild(“Humanoid”).Heartbeat:Connect(onHeartbeat)

This code doesn’t turn the zombie to face the player through? It just makes it walk in a straight line towards them.

Actually, the zombie probably shouldn’t be fully turning towards the player, because they might not be moving in a straight line to them if pathfinding is use. Really, the zombie should turn it’s head and torso to look at the player more naturally, but it depends on what OP wants. Personally, a zombie strafing while looking straight at me just doesn’t look quite right to me.

1 Like

Oh I’m sorry I’m a bit new on the script and stuff. Thanks for telling me.

is there an alternative to this? i don’t want to use instances

Maybe OP wants to make CS style bots or create a more general enemy AI with guns, but idk how a zombie would work at that point unless you want bots with knives come at you, strafing and jumping like CS 1.6 bots or PD2 zombie faction

Humanoids are instances anyway, and it’s not like you need to interact with any extra instances outside of setting ControllerManager.FacingDirection as you can just use the officially-provided example scripts.

This is the easiest way to go that I have discovered, I haven’t come across a method as simple and functional as this.

where do you put the controllermanager? im confused

You should use the Example scripts, which creates the ControllerManager and all the other necessary components for the character of every player that joins.

Does something like this work?

local Speed = 30

game:GetService("RunService").Heartbeat:Connect(function(dt)
	local Future = targetplr.HumanoidRootPart.CFrame + (targetplr.HumanoidRootPart.Velocity * Epitaph + Vector3.new(0, .1, 0))
	local GoalCF = CFrame.lookAt(Character.PrimaryPart.CFrame.Position, Future.Position)
	Character.PrimaryPart.CFrame *= CFrame.new():Lerp(Character.PrimaryPart.CFrame:ToObjectSpace(GoalCF), (1 - math.pow(2, -Speed * dt)))
end)