Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make an enemy creature face towards you?

Hi, so I have a custom enemy I made that walks towards you and attack you but I can't seem to make it face towards me. Instead it faces towards the side. Please help

Script inside Enemy:

local enemy = script.Parent
local hrpOfenemy = enemy:WaitForChild("HumanoidRootPart")
 
local plrsHit = {}
 
local maxDistance = 100
local db = true
 
 
enemy.Head.Touched:Connect(function(h)
 
	    if h:IsA("BasePart") and h.Parent:FindFirstChild('Humanoid') and h.Parent.Name ~= script.Parent.Name and db then
      db = false
		
        
        h.Parent.Humanoid:TakeDamage(20)     
        wait(1)
			        db = true
			end
end)
 
 
while wait() do
    local plrs = game.Players:GetPlayers()
    local closestHRP
    
    for i, plr in pairs(plrs) do
        if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Character.Humanoid.Health > 0 then
			            local hrp = plr.Character.HumanoidRootPart
            local distanceBetween = (hrpOfenemy.Position - hrp.Position).Magnitude
			            if not closestHRP then closestHRP = hrp end
			if (hrpOfenemy.Position - closestHRP.Position).Magnitude < distanceBetween then
            closestHRP = hrp
                
            end
        end
    end
    
    if closestHRP and (hrpOfenemy.Position - closestHRP.Position).Magnitude <= maxDistance then enemy.Humanoid:MoveTo(closestHRP.Position) end
end

Animation script:

local Humanoid = script.Parent.Humanoid
local runanim = Humanoid.RunAnim
local anim = Humanoid:LoadAnimation(runanim)

function onRunning(speed)
	if speed > 0.01 then
		anim:Play()
		end
	end

Humanoid.Running:Connect(onRunning)

1 answer

Try adding BodyGyro to the RootPart of the enemy and use :

BodyGyro.CFrame = CFrame.new(RootPart.Position, closestHRP.Head.Position)*CFrame.Angles(math.rad(DEGREES U WANNA ROTATE),0,0)

Please upvote and lemme know if it helped!

5 comments

0

Sorry, I don't see how that would work since there only 2 things inside that new CFrame when there should be 3

0

U dont need to have 3 value in CFrame of BodyGyro, Try doing it like I wrote without multiplying with Cframe.Angles. If the enemy faces another way, then use Cframe.Angles

0

It says Argument 3 missing or nil

0

try removing CFrame.Angles line

0

Melodic don’t just take code

This discussion is preserved as it was. New answers, comments, and votes are closed.