# How to apply an offset to a vector?

Hi,

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?

here you go

``````--declaring the vector
local Vector = Vector3.new(0,0,0)

--applying an offset of 4 to the Y axis
Vector.Y = Vector.Y + 4
``````

i’m not too sure on you are trying to do but correct me if it’s not what you wanted

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.

This is a little bit hard to explain without access to your code, but basically you want to use the unit of vector difference to rotate preset offset

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?

Here’s a script example of what I meant:

``````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:

3 Likes

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