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

How do i know if a car or a part is in the air?

i want to detect if a car or part is in the air. Is there a function or how do you do it? No scripts because i really cant figure out how.

3 answers

You could use GetTouchingParts() and check if the amount of touching parts is zero. Though beware of this, parts which are actually supposed to collide with other parts should be included in the minimum amount of touching parts. For example a wheel would have a drive chassis or something through it, that would count as a touching part so then you would have a minimum of 1, etc.

1 comment

0

adding to that he can use a Car:GetChildren() and ignore any touching part in that table to possibly get better results.

Checking the position is not the best way to do this. I strongly discourage it. Instead use Raycasting. Raycasting is basically a line that you determine where it starts and its direction. Raycasting can be used in a lot of things and it's very useful and important to learn in Game Development. The topic is kind of heavy if you don't have a good understanding of positions and directions (which fundamentally are the same thing) so here's an article about it for now: https://developer.roblox.com/en-us/articles/Making-a-ray-casting-laser-gun-in-Roblox

and here's something that might help you a bit:

-- we will assume that the PrimaryPart is in the middle of the car and the size of the whole car is 30 which means that half of it is 15 which means that if PrimaryPart is further to the ground than 15 then it would be flying
local halfOfCar = 15 -- a ray can take a lot of performance depending on its distance, the shorter, the faster it will run. but it comes with a catch. that's all it's gonna be able to detect, anything further will be undetectable.
local car = workspace.Car
local primaryPart = car.PrimaryPart -- if it's a model
local DOWN = Vector3.new(0,-1,0) -- UP is Vector3.new(0,1,0) read about the hand rule in game dev
local ray = Ray.new(primaryPart.Position,DOWN*halfOfCar) -- this means that there will be an invisible line coming from the middle of the car and going down to see if there's "ground". and when you multiply a Direction by a number it goes further by that number
local hit = workspace:FindPartOnRayWithIgnoreList(ray,car:GetChildren()) -- this function is the one that makes rays useful, right now ray is like a Vector3, it doesn't do anything useful on its own but we use methods to make it useful. this is the one that detects if there's actually a part (or a ground if you will) that goes from PrimaryPart.Position to DOWN*halfOfCar. we are also using "WithIgnoreList" because this one gives us the option to ignore some stuff (it has to be a table). and we don't want the ray to think that the car is "ground"
if hit then
	print("the ray is telling us that there's a part under the car therefore there's a 'ground'")
end

2 comments

0

I am not the best teacher but I did my best :P. also keep in mind halfOfCar should be higher by 1 stud to compensate for inaccuracies but for the sake of explaining we will just imagine as if everything goes as it should.

0

Also if you need any more help I would be more than happy to do so, just PM me :D though it might be better if you post your questions here so that other people would read it too

Try checking if the y position of the part is bigger than a certain number

local part = game.workspace.part
if part.position.y >= 10 then
	print("part is in the air")
elseif part. position.y < 10 then 
	print("part is on the ground")
end

Alternatively you could use the

.Touched

event

3 comments

0

is there a way if you can detect if its actually in air

0

Inside the if statement, part/model name:Destroy ()

0

You first need to determine what you mean by air. Is air 10 studs up or more? You could also use the touched event, and make a table of all the things that your part is touching, then you need to run through the table in look for objects that your part is touching, if it doesn´t touch anything, it is likely in the air.

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