I’ve been trying to script a mob to follow me and it works pretty good, but there is one issue. How can I make it in front of me? Or behind, on the any side.The issue is that when mob follows me and when stop running, he will go below me like this.
Here is the follow script I am trying to remake. Maybe should I use CFrame or Vector3 for making it next to me?
local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = _M.FollowDistance
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(0.1)
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
Enemy:MoveTo(target.Position, target)
end
end
Should work by adding another check in the loop which checks for the magnitude distance between the player and the mob - if the mob is a certain distance from the player only then it should call the :MoveTo() function.
It looks like something similar to what @vsnry suggested is already built into the script, specifically here to check if the mob is close enough to start following:
if (temp.Position - pos).magnitude < dist then
Try just adding to that statement to stop if it’s too close:
local magnitude = (temp.Position - pos).magnitude
if magnitude < dist and magnitude > 5 then
What vsnry is saying is when you are about to stop the mob, check the magnitude of the position of the mob from that of the player. By doing so and comparing this magnitude by a number (studs), you would produce a range at which the mob simply stops, distancing it from the center
Literally where you do “if target ~= nil then” add another check which looks if the distance is higher than a specific number, if so call MoveTo, if not stop the mob completely.
((target.position-pos) pos is called from function findNearestTorso and I can’t place it inside of the function, because target is in the loop outside of the function.
I believe my way of doing this is better as it does not limit the mob to a circle around the player which could look unnatural if the player looked at the mob and started walking towards it, causing the mob to slide around the circle to behind the player, or keep moving forward with the player.
local offset = 5 -- The offset from the root part (in studs)
while true do
wait(0.1)
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil and (target.Position - Enemy.Position).Magnitude > offset then
Enemy:MoveTo(target.Position, target)
end
end
while true do
wait(0.1)
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil and (target.position-script.Parent.HumanoidRootPart.Position).magnitude > 10 then
Enemy:MoveTo(target.CFrame:PointToWorldSpace((target.position-script.Parent.HumanoidRootPart.Position).unit*10))`
end
end