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
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.
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
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?
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)
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
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.