Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
0
Answered by
oilkas 364 Moderation Voter
4 years ago

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
0
Thanks, this works, but what if thec ar is in another height? jdm4llfem8 94 — 4y
0
wdym oilkas 364 — 4y
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 jdm4llfem8 94 — 4y
0
the part needs to be directly under the car's primarypart to detect it as on ground oilkas 364 — 4y
Ad

Answer this question