Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i detect if a part is in the air?

I made a car and i want to print when the car is in the air. I tried making it so when it touches a part, it prints that it touches and if it stops touching, it also prints something but that's not a good way. anybody know a better way? thanks

1 answer

✓ Accepted answer

You can use raycasting, try this:

local Car = script.Parent -- Put this script in the car model.
local PrimaryPart = Car.PrimaryPart -- Set a primarypart of the car.
local Minimum = 10 -- Minimum height to register when the car is in the air.

while wait() do
	local Position = PrimaryPart.CFrame.P
	local Rotation = PrimaryPart.CFrame - Position
	local Ray = Ray.new(Position,Position - Vector3.new(0,Minimum,0)) -- Ray casted down.
	local Touch, TouchPosition = workspace:FindPartOnRay(ray, Car) -- Ignore car.
	if Touch then
		print("Touching "..Touch.Name.. " at "..TouchPosition)
	else
		print("In air")
	end
end

4 comments

0

Thanks, this works, but what if thec ar is in another height?

0

wdym

0

with your script, if a car is on a baseplate and its on the air it works, but if the car is ontop of another part it already detects that its in the air

0

the part needs to be directly under the car's primarypart to detect it as on ground

This discussion is preserved as it was. New answers, comments, and votes are closed.