NPC MoveTo Player. How do I make the NPC keep at a certain distance?
Asked by
6 years ago Edited 6 years ago
im using a script to make an NPC follow the player.
Im trying to make the NPC follow the player, but stay at a certain distance from the player.
like this...
https://imgur.com/a/fXwOUdL
if the player walks away, the NPC gets closer, if the player gets closer, the NPC moves farther away...to maintain the same distance from the player
01 | local Mroot = script.Parent.HumanoidRootPart |
02 | local h = script.Parent.Humanoid |
03 | local Mspawn = script.Parent.Parent.MobSpawn.CFrame |
08 | for _,v in next , game.Players:GetPlayers() do |
10 | local char = v.Character |
11 | if char:FindFirstChild( "Humanoid" ) and char:FindFirstChild( "HumanoidRootPart" ) then |
12 | local pRoot = char.HumanoidRootPart |
13 | if (pRoot.Position - Mroot.Position).magnitude < = 40 then |
23 | local player = findplayer() |
25 | local stayat = CFrame * CFrame.new( 0 , 0 ,- 20 ) |
26 | h:MoveTo(player.Character.HumanoidRootPart.Position) |
ive tried doing this...
2 | local player = findplayer() |
4 | local stayat = player.Character.HumanoidRootPart.CFrame * CFrame.new( 0 , 0 ,- 20 ) |
The NPC keeps the distance away from the player, but it tries to stay directly in front of the player...
like this...
https://imgur.com/a/GY57ohj
https://imgur.com/a/Iwk9mq2
how do i make the NPC walk straight at the player, but stop at a certain distance...and if the player gets closer, walk away from the player, and remain the distance