Hi developers!
I’ve been trying to make a boss fighting game, and right now I’m working on just basic enemies. A core part in making enemies is having them chase you. I’ve run into a problem, however. The movement of the enemy is somewhat staggered when you get close enough, as seen here.
I’d like to make this better, so you can’t keep making distance with the enemy. A fantastic example of this pathfinding would be a pretty solid bossfighting game, Cheeseburger Crusade. I’ve recorded a short sample of what I’d like to accomplish here.
Before I forget, here’s the current chase code I have:
local targetDistance = math.huge
local stopDistance = 2
local function findPlr()
local plrs = players:GetPlayers()
local nearestPlr = nil
local distance = nil
local direction = nil
for _,player in pairs(plrs) do
local character = player.Character
if character then
local distanceVector = (player.Character.HumanoidRootPart.Position - root.Position)
if not nearestPlr then
nearestPlr = player
TargetPlayer.Value = nearestPlr
distance = distanceVector.Magnitude
direction = distanceVector.Unit
elseif distanceVector.Magnitude < distance then
nearestPlr = player
distance = distanceVector.Magnitude
direction = distanceVector.Unit
end
end
end
return nearestPlr,distance,direction
end
runService.Heartbeat:Connect(function()
local nearestPlr, distance, direction = findPlr()
TargetPlayer.Value = nearestPlr
if nearestPlr then
if distance <= targetDistance and distance >= stopDistance then
humanoid:Move(direction)
else
humanoid:Move(Vector3.new())
end
end
end)
As I say in all of my posts, any help would be greatly appreciated.
After taking a few looks, it appears that the boss constantly looks at the player’s position, so that could tie into something. Other than that, I have no clue!