Making an NPC stick behind the player at all times

I’m working on a game that plays similar to the Mario & Luigi RPG series, where the second playable character is always following behind the player in the overworld outside of battle.

The general idea is that the second character will stick behind the player and follow them around automatically, but certain actions (such as jumping) will require player input. The player should not be able to split up with the second character normally, the Mario & Luigi series prevents it by stopping the player from moving if they attempt to move when the second character cannot follow them.

Here’s a video that should hopefully showcase what I’m aiming for (timestamped at 41:40 because it’s a playthrough of the entire game lol)

2 Likes

You could create pathfinding waypoints on the first character every time they move, then have the second character follow them using Humanoid:MoveTo.

3 Likes

I’m just gonna add on to what @Cozmicweb has stated.
Basically on every frame, you need to check the distance between the Player and the NPC.
If the distance is greater than a threshold, compute a path to the player and move the humanoid.

RunService.Heartbeat:Connect(function(): ()
    --// example, replace with actual code lol
    local Distance = (Player.Position - NPC.Position).Magnitude;
    if (Distance > DistanceThreshold) then
        --// compute the path and move to the player
        --// make sure not to compute the path too many times
    else
        Humanoid:MoveTo(RootPart.Position) --// stops the character
    end;
end);
3 Likes

like this?


l

2 Likes

Ngl, I think I nailed it.

1 Like

Yeah something like this is what I’m thinking of

1 Like

If you want the ‘NPC’ to be another player, it’s 100% possible, but it’s a bit hard to get it to work. I believe for players, it’s better to weld them because physics constraints replicate very smoothly.

Here’s the place file:
npc_follow_player.rbxl (64.6 KB)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.