Raycasting distance

I have a part and I would like to see how high it is from the ground (The part under it) with the front-facing down, I think I need to use raycasts? I could use any help!

5 Likes

Yup! You can do this easily with raycasting.

First, create a ray with the origin position set as the parts position and the direction set downwards:

local MaxDistance = 10 --Maximun distance the ray can go
local Part = workspace.Part --Your part from where you want to raycast
local R = Ray.new(Part.Position, Vector3.new(0,-1*MaxDistance,0)) -- First parameter is the origin and second is the direction. Since it is going downwards, the Y coordinate of the direction has to be negative

local PartFound, Pos = workspace:FindPartOnRay(R)
--Pos is the Vector3 value of where the ray hit a part at.


So now, to get the distance between the Part and the other part under it (the ground), just get the difference between the two Y coordinates of the origin and where the ray hit the ground.

So:

local DistanceToGround = Part.Position.Y-Pos.Y
print(DistanceToGround) -- This prints the distance in studs between the part and the ground

Hopes this solves your question :slight_smile:

14 Likes

To find how high a part is from the ground using raycasts, it’s pretty simple.

First you want to create a raycast with the appropriate arguments:

local groundDistanceRay = Ray.new(part.Position, Vector3.new(0, -500, 0)

The first argument of the raycast is the origin which is where the ray will begin, and the second argument is the direction which is a vector direction of where the ray will point. Since we set the direction as -500 on the Y axis, it will point down for 500 studs.

The next step to finding the distance from the ground is to use workspace:FindPartOnRay.

local groundDistanceRay = Ray.new(part.Position, Vector3.new(0, -500, 0)
local part, position = workspace:FindPartOnRayWithIgnoreList(groundDistanceRay, {part}) -- just makes sure the ray doesnt hit itself

Now that you have the position, you can simply find the distance with some basic vector math.

local groundDistanceRay = Ray.new(part.Position, Vector3.new(0, -500, 0)
local part, position = workspace:FindPartOnRayWithIgnoreList(groundDistanceRay, {part}) -- just makes sure the ray doesnt hit itself

local groundDistance = (part.Position - position).Magnitude
print(groundDistance)
7 Likes

Thanks for all the help guys! :smiley:

2 Likes

Why is this not working ?

local MaxDistance = 200 --Maximun distance the ray can go
local Part = script.Parent --Your part from where you want to raycast

while true do
wait(1)
local R = Ray.new(Part.Position, Vector3.new(0,-1*MaxDistance,0))
local PartFound, Pos = workspace:FindPartOnRay(R)
local DistanceToGround = Part.Position.Y-Pos.Y
if DistanceToGround <100 then
game.ReplicatedStorage.DefaultChatSystemChatEvents.SayMessageRequest:FireServer(game.Players.LocalPlayer.Name…" ,"…“please pull up”, “All”)
end
end

this is a helicopter script

Hey, how do I perform a raycast operation and only get the distance from the RayOrigin to the intersecting part? Just the distance and nothing else, and like e.g print the distance integer out into the console with print() using the Raycast() command?

Follow raycast documentation: Raycasting | Documentation - Roblox Creator Hub

workspace:Raycast() returns a RaycastResult from which you can get RaycastResult.Distance.