Mob in front of you

Hello again.

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.

I don’t want that. It looks ridiculous! How am I going to make it stays in front, behind, left, right on the any side, just not below me?

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

Thanks!

2 Likes

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.

1 Like

I am a little confused by that? I just need it to be a little back from the player.

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

I’m sorry I’m confused. Is this problem occuring when the mob stops after following you or is it when the mob is yet to start it’s pursuit?

It works, but still it goes under me and then when I move a bit it will stay and then follows me again.

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

You are moving the NPC to the position of the player’s torso. You need to offset that by the distance you want it to stay from the player.

try something along the lines of:

Enemy:MoveTo(target.CFrame:PointToWorldSpace((target.position-script.Parent.HumanoidRootPart.Position).unit*10))

this would send the NPC to a point 10 studs away from the target at a point directly between the target and the NPCs current position.

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.

Sorry, I scrolled down and saw the rest of the code. Fixed up my original response.

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.

1 Like

Modifying this part by adding/subtracting a vector should stop it from getting right under

Now my mob goes around me in circle and slightly coming to me. I need to stop him somehow.

Do as vsnry said and check that the distance between the mob and the target is greater than 10 before moving the mob on the if target ~= nil then line

I believe this would work

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

That won’t work either. My mob is just going below me and I am on the edge of his head.

I don’t know whether this will work or not because I’m on mobile but maybe try this?

while wait(0.5) do
    mob.Humanoid:MoveTo(Character.HumanoidRootPart.Position - Character.HumanoidRootPart.CFrame.LookVector * 3)
end

Sorry without the loop, just add the center part

That would cause it to only ever be behind the player.

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

Adjust distances as needed