Hugging terrain by following terrain normals

Hey all, I’m pulling my hair out.

I have a vehicle configured to use RichAI and it works great. However, it doesn’t hug the terrain well…the vehicle stays horizontal up and down hills and generally looks silly. I have a script that I’ve procured(plagerized) from the net and it works great following the terrain normal. Each of the 4 corners of the vehicle move up and down properly over terrain. However, the RichAI script and the terrain hugging script seem to step on each other’s toes. The rotation from each scripts won’t play nice. Unfortunately, I don’t understand how the terrain hugging works. Could someone help me to get this integrated and play nice with RichAI? Here is the code:

    void RunUnitGroundPhysicsTank()
    {
        RaycastHit lr;
        RaycastHit rr;
        RaycastHit lf;
        RaycastHit rf;
        RaycastHit c;

        GetUnitBounds();

        Physics.Raycast(unit_rightBackEdge, Vector3.down, out rr, unit_GroundMask);
        Physics.Raycast(unit_leftBackEdge, Vector3.down, out lr, unit_GroundMask);

        Physics.Raycast(unit_rightFrontEdge, Vector3.down, out rf, unit_GroundMask);
        Physics.Raycast(unit_leftFrontEdge, Vector3.down, out lf, unit_GroundMask);

        upDir = (Vector3.Cross(rr.point - Vector3.up, lr.point - Vector3.up) +
                Vector3.Cross(lr.point - Vector3.up, lf.point - Vector3.up) +
                Vector3.Cross(lf.point - Vector3.up, rf.point - Vector3.up) +
                Vector3.Cross(rf.point - Vector3.up, rr.point - Vector3.up)
                ).normalized;
        Debug.DrawRay(rr.point, Vector3.up);
        Debug.DrawRay(lr.point, Vector3.up);
        Debug.DrawRay(lf.point, Vector3.up);
        Debug.DrawRay(rf.point, Vector3.up);

        transform.up = upDir;

    }

Anyone? Any terrain hugging code that works with A* Pathfinding project? Even a purchased asset. Thanks!

Hi

I think if you do the terrain hugging during LateUpdate() then it will override the RichAI.