This may sound like a simple question, but I just can’t figure it out. I have a point that players can approach from all distances and positions in the map. I’m using pathfinding to walk the player there. However, in order to avoid collission with the object, I want to apply a little bit of an offset, so the player stops at a point before it hits the object. How would I achieve this?
No this is not what i need, because it does not keep the position of the player in mind. The player can come from any direction so if I apply an offset of (-1, 0, -2) or something, it will work when the player comes from the bottom left of the object, but if a player comes from the top right, it will only try to walk further.
i’m stupid but.
So, you want that a pathfinder controlled player stops when an object is near to try to avoid collision?
If it’s wrong i think i shouldn’t reply here anymore because of my stupidity.
I’m not sure if this is what you wanted but you could get the distance between the player and the point and check if it’s close enough to break the loop that handles the pathfinding?
local plr = game:GetService("Players").LocalPlayer or game:GetService("Players").PlayerAdded:Wait()
local target = workspace:WaitForChild("Target")
local presetOffset = Vector3.new(10,0,10)
local function gotoWithOffset()
local plrPos = plr.Character.PrimaryPart.Position
local differenceV = plrPos - target.Position
differenceV = differenceV.Unit
local rotatedOffset = presetOffset * differenceV
local targetPos = target.Position + rotatedOffset
plr.Character:WaitForChild("Humanoid"):MoveTo(targetPos)
end
Here is a video of this script bound to a UI button:
everytime your MoveToFinished is called check the distance to the object
if its less than or equal to a radius (distances are in a sphere around the object) then stop the pathfinding, this should stop moving the player
-- how to get distance from two positions
(vec1 - vec2).Magnitude
-- how to check distance
local dist = (vec1 - vec2).Magnitude
local radius = 10 -- in studs
if dist <= radius then
end
he said he was using pathfinding so MoveTo is called for each node of the path so if you check the distance for each node it will work fine. not perfectly but fine