Is it possible to detect if a Part is close to the Ground?

use

workspace:Raycast()
4 Likes

You can cast a raycast to check weather there is ground under, and then check the distance in order to emit particles or not.

You can look more into it in the docs

local partvector = script.Parent.Position -- Position of your part
local direction = Vector3.new(0, -100, 0) -- Angle to look at

local result = workspace:Raycast(partvector, direction)

if result then
	print("Instance:", result.Instance.Name)
	print("Distance:", result.Distance)
        -- And then whatever you want to check.
end

image
image

4 Likes

And how do I detect if the part is like 10 studs up?

Tried to set if the distance is 10 but still didn’t worked.

Raycasting is like a beam. First variable is the beginning point and the second is the direction. You can make the first one the Position of the vehicle, and the other one something like Vector3.new(0,-20,0)
and if the raycast isnt nil it’ll return the object, the position of impact and the Normal. Please also look into RaycastParams for blocking objects or only whitelisting them for your raycast.

This only goes 20 studs down for when searching for impacts.

Shiawase has a perfect reply for you. The studs don’t matter. Set that up fly where you want to be and check the number. That or lower is the trigger to the effect. If that is a bit lower then you wished just go with it. You could just simply check the Helicopter height and trigger the effect from any height. The advantage here is you can tell you’re over sand, do a sand effect or cement do no effect. To me that outweighs being totally perfectly where you want the height … gl

1 Like

Yea this isn’t really working for me. I’m gonna go find another alternative

You haven’t posted any code that people can help you with. Did you print any output so we can see why it isn’t working?

You have to loop and check again and again, the ray cast just checks one point in time, you have to check again and again. Maybe that is the point you have missed?

1 Like

I still haven’t found any solutions to detect if it’s like 5 or 10 studs in the air.

This will work. If you post your code and the result you see, someone can tell you why it isn’t working for you.

Try to use print statements to see what it is doing.

Try adding this to your code so you can see what is happening from the output. Change the rayOrigin to whatever part’s position on your vehicle where you want the ray to start.

local rayOrigin = Vector3.zero
local rayDirection = Vector3.new(0, -100, 0)

local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

if raycastResult then
	print("Instance:", raycastResult.Instance)
	print("Position:", raycastResult.Position)
	print("Distance:", raycastResult.Distance)
	print("Material:", raycastResult.Material)
	print("Normal:", raycastResult.Normal)
else
	warn("No raycast result!")
end

this is from:

You can use an attachment position if needed to start the raycast outside of the vehicle part.

This script constantly checks the distance from a part named “Part” to the ground using a raycast. If the distance is less than or equal to 10 studs, it prints the distance. The script utilizes the RunService to repeatedly update the part’s CFrame (position and rotation) and trigger the raycast check.

It won’t continuously print the result when the distance goes below 10 studs, but it serves as a stepping stone to guide you in the right direction.

local RunService = game:GetService("RunService")

local part = workspace.Part
local CFrameValue = Instance.new("CFrameValue")
CFrameValue.Name = "PartCFrame"
CFrameValue.Value = part.CFrame

local function onChanged()
	local partPosition = part.Position
	local direction = Vector3.new(0, -100, 0)
	local result = workspace:Raycast(partPosition, direction)

	if result and result.Distance <= 10 then
		print("Distance to ground:", result.Distance)
	else
		print("Not below 10 studs") -- Testing purposes, u can delete it
	end
end

local function onHeartbeat()
	CFrameValue.Value = part.CFrame
end

CFrameValue.Changed:Connect(onChanged)
RunService.Heartbeat:Connect(onHeartbeat)

It’s not detecting the parts position.

However this did worked but can’t seem to detect the studs.

local partvector = script.Parent.Parent.Wind
local direction = Vector3.new(0, -100, 0)

local result = workspace:Raycast(partvector, direction)

while true do
	wait()
	if result then
		print("Instance:", result.Instance.Name)
		print("Distance:", result.Distance)
	end
end

It’s still saying “Not below 10 studs”

And It’s still not reacting even if the Helicopter is like 10 or even 1 feet above like this:

Screenshot 2024-01-26 174607

You’re only raycasting once, also your direction is -100 studs down so it’ll be active from way higher than you want (?)

Try something like this and adjust for whatever else purpose:

local partvector = script.Parent.Parent.Wind
local direction = Vector3.new(0, -10, 0)

local result = workspace:Raycast(partvector.Position, direction)

while task.wait(0.5) do
result = workspace:Raycast(partvector.Position, direction)
	if result then
		print("Instance:", result.Instance.Name)
		print("Distance:", result.Distance)
	end
end

You should really read the documentation for raycasting by going to the docs Shiawase replied with.

You can look more into it in the docs

PS: if it starts hitting the helicopter itself please bring that up

Hey,
I was able to something that I think is like what you mean:

I created a topic for it but based on your question

1 Like

if you want to measure the distance between 2 points use magnitude. This is way quicker than raycasting

Stefex is right you are only checking the ray one time and looping through the same result. Try dkevinb’s script or at least move the raycast into your loop:

1 Like