Hum:MoveTo() function moving opposite direction from player?

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.

Swap the two positions in the moveTo function

the MoveTo() function is really buggy and outdated, i recommend using the pathfinding service instead

also for checking if the player is in range and in the bot’s vision, use raycast

3 Likes

Use Unit instead of LookVector

local difference = (targetPosition - npcPosition)
local direction = difference.Unit
Humanoid:MoveTo(targetPosition + direction * range/1.2)

Pathfinding service finds the path. You would still need to use MoveTo to go on that path.

2 Likes

Tried this but it doesn’t account for staying away for the Range. What I mean is it keeps trying go to the EXACT point of the character, so it just walks into it.

I’m not a pro at this, but isn’t CFrame.new(0, 0, -1) a world space distance?
Does the NPC move the same direction in the workspace each time, or does it always move away from the player?

Should you be adding CFrame.new(0, 0, -1) instead of multiplying it?

Change + direction * range/1.2 to - direction * range/1.2

tbh i dont think the gyro is doing anything at all, have you tried disabling humanoid.AutoRotate? if it (gyro) rotates the npc then it is working.