I have a simple 3d box car with a rigidbody, collider, and move with rb.MovePosition. How do I force the car to be in a stand-up straight position while falling and not spin in the air? If I use rigidbody rotation constraints to freeze rotation, the car won’t line up with the ramp when going up.
Here is a script (warning pseudocode) that freezes the roation in the air when the car detects it is in freefall. Should show you the basics of how to implement something like that:
using UnityEngine;
public class CarFreefallConstrains : MonoBehaviour
{
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (rb.velocity.y < 0f && !IsGrounded())
{
rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
}
else
{
rb.constraints = RigidbodyConstraints.None;
}
}
private bool IsGrounded()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 1f))
{
return true;
}
return false;
}
}