I’m making a hostile NPC system where the NPC can chase you. However, I am having an issue with the Humanoid:MoveTo() function.
-- Target: the character the mob is aggro'd onto (the player's character)
-- Mob: the character of the mob
-- MobInformation.Range: a constant which determines the minimum distance between Target and Mob while pursuing
Humanoid:MoveTo(TargetHumanoidRootPart.Position - CFrame.new(HRP.Position, TargetHumanoidRootPart.Position).LookVector * MobInformation.Range/1.2)
To allow the mob to face towards the character, I use a BodyGyro which uses the following code and is being updated every 0.05 seconds
local function LookAt()
MobGyro.P = 5000
MobGyro.D = 50
local lookAt = Target.Value.HumanoidRootPart.Position
local at = (Mob.HumanoidRootPart.CFrame * CFrame.new(0,0,-1)).Position
lookAt = Vector3.new(lookAt.X,at.Y,lookAt.Z)
MobGyro.CFrame = CFrame.lookAt(at, lookAt)
end
When I get close to the mob with this code, the following occurs:
Similarly, jumping with the same code makes this occur:
Not really sure what’s going on with the MoveTo() function, but if anyone knows why it goes the opposite direction when getting closer I’d appreciate some insight on how to fix it. I believe there is some obscure mistake I’m making regarding the CFrame of MoveTo() and possibly the Gyro.