How to check if a part is upside down?

I am trying to make a car-flip script, and for that i need to determine if it is upside down.

I’m thinking of doing something like:

if car.Orientation.X < 90 or  car.Orientation.X > -90 or car.Orientation.Z < 90 or  car.Orientation.Z > -90 then
--flip
end 

But surely there must be a better way to do this rather than running checks

3 Likes

That’s probably exactly how I would do it

3 Likes

Fair enough, thanks for the reply

2 Likes

You can cast a unit ray down and check if the position is smaller than the actual position:

if Car.Position.Y + Vector3.new(0, Car.CFrame.UpVector.Unit) < Car.Position.Y then
    -- do something
end
3 Likes

We will need two vectors:

Let’s create a third vector p2 = position + UpVector
Compare the Y coordinates of position and p2. If p2.Y is < position.Y then the part is upside down.

function isUpsideDown(part)
	local UpVector = part.CFrame.UpVector
	local position = part.Position
	return position.Y > (position + UpVector).Y
end

Simple vector addition makes this very efficient.

13 Likes

Sorry to bump this old thread, but I noticed that the solution provided got the position involved needlessly.

(A > A + B) rather than (0 > B).

Here’s a simplified approach that doesn’t require the part Position to be used.

(it simply checks if the UpVector is negative)

function isUpsideDown(part)
	local UpVector = part.CFrame.UpVector
	return  UpVector.Y < 0
end
4 Likes