EDIT: my other comment is awaiting approval so this won’t make sense until you see that one, but I uploaded a demo project.
And here’s the answer I wrote before I decided to just make the demo:
It sounds like you could use some insight into how vectors work if you want to refine it in the future. This is a really important thing to learn in game dev so I personally think it’s a time investment worth making.
There’s some good info here: Vector math — Godot Engine (stable) documentation in English
It’s extremely useful to learn about things like dot and cross products and what they mean, but for this application you only need to understand how the vectors relate to vertical and the horizontal plane, which is very easy.
For instance, if you can figure out what the foward and up vectors of the vehicle are, you can more easily figure out how to adjust them to an orientation that makes sense. I believe looking at the docs that the vehicle’s forward vector is equal to -transform.basis.z, but it would depend on how the transform is oriented with respect to the vehicle itself, which depends on how you made it.
I personally would figure out which vector points in which direction then create properties for vehicle.RIGHT, vehicle.FORWARD and vehicle.UP that simply retrieve the appropriate vectors from transform.basis. Then once you have those it becomes really easy to learn exactly what the vehicle is doing. If need be you can always create an empty Node3D object that is parented to the vehicle and provides convenient vectors. This is pretty cheap performance-wise.
Then you could take the vehicle.FORWARD vector and project it to the horizontal plane. In this special case it’s very easy to do this by just zeroing out the vertical component (we’ll call Y vertical for now but it depends on your settings) and then normalising the vector afterwards. In the ultra-rare edge case that the vector is perfectly vertical you can just pick an arbitrary world direction.
Then, have the vehicle look at this resultant vector with its up vector defaulting to world up. This should make the vehicle face the direction it was already facing but upright. This would work to find a good teleport direction for the reset. Again if the vehicle transform isn’t facing in a good direction, you can always create an intermediate helper transform. Make the transform look the direction you want, then make the vehicle look at whichever pair of vectors on the helper makes the vehicle point where you want.
If you wanted to do this with a physics force rather than a teleport, just check the vehicle.UP vector and if the UP.y component passes some positive value like >0.5, LERP the force or torque to zero. To prevent the flip mechanic being abused, just disable the function unless UP.y is below this value. Adjust the cutoff value to whatever feels right.
To find the correct direction to flip it in, you could point the torque in the direction of the vehicle’s forward vector, then choose a clockwise or counterclockwise force depending on which is closest to upright already. You would determine this by taking vehicle.RIGHT.y and checking its value. If it’s positive apply a clockwise force around vehicle.FORWARD, otherwise counterclockwise (if this is wrong just flip the values, it’s what I usually do rather than the rigorous maths).
Alternatively, you could apply a force to push the high side of the vehicle “down” relative to the up vector (either take the down vector, or the negative of the up vector). Imagine a big boot kicking down on the vehicle’s running boards to push it over, then adjust the push to be parallel to the vehicle down each frame until it’s righted. If vehicle.RIGHT.y is positive, push on the right side, otherwise the left. This is probably the easiest way.
I like the physics flip idea because it gives the player the ability to shove the vehicle until it’s out of whatever situation flipped it. Sometimes a reset leaves you stuck and there’s not much you can do.