Determine if part is midair?

I have a system that anchors parts in place if their velocity is small enough, as if a bunch of parts fall on each other, they start jumping around and eventually lag the game. However, sometimes my script anchors parts while they are in the air. I know there’s a way to check if the player is in the air, and I was wondering if there was a way to get the same information, but for baseParts and meshParts. Any help is appreciated!

2 Likes

You could create a raycast at the part’s position subtracted by the UpVector of the part’s CFrame, so it will be one stud below the part’s bottom surface. Then you check if the ray is nil. If not, there’s something below.

Downside: only puts the ray on the bottom surface. If the part is oriented to have it not face down, it will not work right. That can be fixed by using the appropriate vector and math instead of only subtracting the UpVector

2 Likes

Why Not trying raycasting? If it returns no part, it will indicate that the part might be still be in the air

How to detect if a player is in the air or not - Help and Feedback / Scripting Support - Developer Forum | Roblox

1 Like

Yeah, that’s why this method won’t work for me… Parts are getting flung and almost always land in a different position/orientation.

1 Like

Then you could more easily just subtract one from the part’s position on the Y.

2 Likes

Yes, but the only problem is that with this, I could only easily check if a part is x studs away from another specific part, like the baseplate. This way, I can’t easily check if the part is on top of other parts.

1 Like

Maybe take a look at Touched and TouchEnded events? A part is mid-air if no other parts are touching it.
This code sample might help you.

If you use a raycast (like I mentioned in my first reply), the origin as the part’s position, the other position being the part’s position subtracted by one on the Y, and your raycast params, you then check if raycast == nil then. If it is nil, there is nothing under the part. This determines if the part is midair or not.

1 Like

I was thinking that an alternative to this could be :GetTouchingParts(), where if it returns a table with no values, then I know it’s in the air. Achieves a similar result to raycast, but I’m not sure which will be more efficient.

1 Like

Counting touching parts using the Touched and TouchEnded events is more efficient than casting a ray every heartbeat.

1 Like

Raycasts are always global to the world. It will not be only on the bottom surface of the part; just situated from the center of the part. You just make a 1-5 stud ray depending on the size of the part and exclude the part in the cast. The orientation doesn’t matter because rays are separate.

2 Likes