Getting the distance of the part's front face to the ground

Hello! I’ve been trying to design a script that auto calculates the distance between a part and another part in the direction the casting part is facing. I just don’t know where to start… I’ve tried casting from the Cframe.LookVector, but with no luck.

It’s really hard because if I want to rotate the part, I need to ensure it casts forwards and isn’t based on a world rotation. Any pointers would be great! (Pun intended)

1 Like

I don’t fully grasp what you are asking. Could you try drawing an image?

Try this code out:

local part1 = script.Parent

local function GetPartsInFrontRay(start)
	local params =
	params.FilterDescendantsInstances = {start}
	params.FilterType = Enum.RaycastFilterType.Blacklist

	local vector = start.CFrame.LookVector
	local ray = workspace:Raycast(start.Position, vector * 40, params)

	if ray and ray.Instance then
		return ray.Instance

while task.wait(.05) do
	local part = GetPartsInFrontRay(part1)

	if part then
1 Like


Oh, that’s how you tie in the look vector!
Incredible, thank you :slight_smile:
Although I may be dumb, how would I calculate the distance between the hit part and the start position using this method?

Nevermind! You can do:
origin.Position.Magnitude - part.Position.Magnitude
To get the distance between the two parts

1 Like

If the magnitude of origin is lower than the part, the distance will be negative. It should be:

(origin.Position - part.Position).Magnitude