Get Closest Path Offset

Hello, I’m adding an offset to my path finding. Only problem is I can’t just do + Vector3.new(3, 0, 3) because it will always be that. So how can I get the closest position within the radius.

So basically imagine a circle around the npc. How would I get the closest position in the circle?

Current Script:

local humanoid = npc:WaitForChild("Humanoid")
local rootPart= npc:WaitForChild("HumanoidRootPart")
local rotatedOffset = (targetRoot.Position - targetRoot.Position).Unit * offset
local Goal = targetRoot.Position + rotatedOffset

The problem with this script is there’s no offset.

Thank you.

3 Likes

I’m confused here. Do you want a (random) point within a circle with a given radius. The closest position in the circle, to the NPC would always be the current position of the NPC?

Or do you mean the position in the circle that is closest to the player?

Draw it if you can

3 Likes

image
So the red point is the target position. The green is the npc position. The black is the goal position. How would I get the the goal position?

Thank you.

2 Likes

I see. Try this function. It uses the lerp function (Linear interpolation) to get the point that is between a, the target, and b, the NPC, offset by the radius, r.

function GetPoint(a : Vector3, b : Vector3, r : number)
	return (a:Lerp(b,((b-a).Magnitude-r)/((b-a).Magnitude))) -- Lerp between a and b, with alpha value dictated by radius (Must be between 0 and 1)
end

Let me know if you have any questions!

4 Likes

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