Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make an AI Bot stay behind the player?

Asked by 5 years ago

Hello, I have this script which makes an NPC Bot follow a player. I attempted to make the bot always stay behind the player at a good distance but I had an issue if the player is facing a different direction? How can I make it always go behind the Player no matter the way they are facing.

while FollowMaster == true do
    if (game.Players.CrazyCorrs.Character.HumanoidRootPart.Position-script.Parent.HumanoidRootPart.Position).Magnitude > 5 then
        script.Parent.Humanoid:MoveTo(game.Players.CrazyCorrs.Character.HumanoidRootPart.Position-Vector3.new(0,0,-2.3))
    end
    wait()
end

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

The script is cut but anyway I would've instead used CFrames, just do this instead:

local howFar = CFrame.new(0,0,-2.3)
local humanoidRP = game.Players.CrazyCorrs.Character.HumanoidRootPart
script.Parent.Humanoid:MoveTo((humanoidRP.CFrame*howFar).position) -- now it will be more local than global (more related to the character than everything else). don't forget to do the ".position" because the result is a CFrame (the multiplication of a cframe and a cframe = another cframe) and MoveTo only accepts Vector3.
0
Thanks CrazyCorrs 98 — 5y
0
So glad I was able to help you. I forgot about mentioning an article if you wanna learn more about CFrames so here you go: https://developer.roblox.com/en-us/articles/Understanding-CFrame GGRBXLuaGG 417 — 5y
0
And if your question was answered please mark it as so :D GGRBXLuaGG 417 — 5y
Ad

Answer this question