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

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
if dist <= radius then
end
1 Like

you cant directly edit values inside a vector3. you must add it by another vector or set it to a new vector

MoveToFinished is called AFTER your character is near the destination, just put it inside a loop instead.

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.