Moving part across a line based on another's Position

I have a line between 2 parts with one part in the middle.
point
I want the red part in the middle to run accross the line with a position parallel to the car’s. Sort of like this:


Any help is appreciated.

1 Like

So like this?
image

Here’s an interactive demo I found, go see if it’s what you’re looking for. There’s tons of resources on this.
Distance of a Point to a Segment - Wolfram Demonstrations Project.

2 Likes

Yes, that is what I want. This is the closest I’ve gotten so far:


This is the function I used:

local function GetRayClosestPoint(Origin, Direction, Point)
	local A, B = (Origin - Point), Direction.Unit

	local V = A - A:Dot(B) * B.Unit

	return Point + V
end
GetRayClosestPoint(lineEnd.Position,lineEnd.Position*-lineCFrame.LookVector*(lineStart.Position-lineEnd.Position).Magnitude,car.Position)

I don’t understand the nature of your question. It seems like you already have a solution. Is there anything unsatisfactory with it?

Try this:

local function closestToSegment(a: Vector3, b: Vector3, p: Vector3): Vector3
	if a == b then return p end
	local v: Vector3 = b - a
	local u: Vector3 = a - p
	local t: number = - (v:Dot(u))/(v.X^2 + v.Y^2 + v.Z^2)
	return (1-t)*a + t*b
end

a and b are the positions of the two parts, p is the position of the car, and it returns the position on the line

2 Likes

Worked perfectly, thank you very much.

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