How can I orient my vehicle parallel to the ground?

I have a vehicle in a simple game prototype that can move around a 3D world using two thrusters (left and right, each of which add forward thrust and a small amount of rotation in the appropriate direction). The X and Z axis are left/right and forward/backward in my world, and the Y axis is up/down (my simulated gravity is negative Y).

Here’s my problem: I cannot figure out how to orient my vehicle parallel to the ground on an inclined surface. I’ve tried every code snippet I could find on this site and in the Unity forums, and none of them seem to work. This is the code I’m using for vehicle movement, sans any incline rotation code:


public float ThrusterForwardPower;
public float ThrusterRotationPower;
public float Gravity;
protected RaycastHit hit;

void FixedUpdate()
{
    // Apply gravity
    if (!Physics.Raycast(transform.position, -Vector3.up, out hit, 1))
    {
        this.rigidbody.AddRelativeForce(
            transform.up * Gravity * Time.deltaTime
        );
    }

    // Check input and apply forces
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        // Rotate left and apply forward force
        this.rigidbody.AddTorque(Vector3.down * ThrusterRotationPower * Time.deltaTime);
        this.rigidbody.AddRelativeForce(Vector3.forward * ThrusterForwardPower * Time.deltaTime);
    }

    if (Input.GetKey(KeyCode.RightArrow))
    {
        // Rotate right and apply forward force
        this.rigidbody.AddTorque(Vector3.up * ThrusterRotationPower * Time.deltaTime);
        this.rigidbody.AddRelativeForce(Vector3.forward * ThrusterForwardPower * Time.deltaTime);
    }
}

I was especially excited to find this answer, which seems to be the “holy grail”, but I tried it and it produces really weird results with my code:

http://answers.unity3d.com/questions/27340/rotating-an-object-to-equal-normals-of-object-belo.html

I suspect it has something to do with the fact that I’m using AddTorque, but even when I removed that torque code and just used transform.Rotate, the vehicle still behaves oddly. The vehicle is sometimes angled correctly, but other times it’s not, and it’s also like the rotation is “fighting” against my thruster rotation around the Y axis sometimes.

Finally, to help illustrate what I’m trying to do, here’s a screenshot of the vehicle on an inclined surface, showing how the vehicle isn’t appropriately parallel to the ground:

alt text

Can anyone help point me in the right direction?

Ok, if you want to try the normal way Orient vehicle to ground normal (terrain hugging) - Questions & Answers - Unity Discussions