How do you rotate a NavMesh object to match the terrain's angle?

Hi,

I’m trying to rotate an object along its X axis that’s using a NavMesh component. The AI is already finished and works fine. The problem is that the NavMesh object needs to match the terrain’s angle to avoid looking unrealistic like below. The object seems to always stay at 0 degrees regardless of it being rotated.

I’m using the code below (JS) to get the angle of the front and back of the model using raycasts which then aligns the model according to the angle of the terrain. The problem is that for some reason Unity will not let me rotate an object that’s using a NavMesh component and I’m not sure why. I hope I’m mistaken and it’s something on my part.

I’ve tested this out with an object not using NavMesh and it works perfectly fine so I’m not really sure what the issue is. If anyone can point me in the right direction it would be greatly appreciated.

function FixedUpdate()
{
	var hit: RaycastHit;
    if (Physics.Raycast(transform.position + transform.forward, Vector3.down, hit)){
        var fHit = hit.point; // get the point where the front ray hits the ground
        if (Physics.Raycast(transform.position - transform.forward, Vector3.down, hit)){
            var bHit = hit.point; // get the back hit point
            transform.forward = fHit - bHit; // align the object to these points
        }
    }
}

32856-issue.png

Make sure your model is on a child, and orient that, don’t orient the navmeshagent. Try to use good organizational skills. You don’t want 30 components on one gameobject, finding anything in the inspector would be a pain. Controls should be the root, housing your AI and such, your main controls for the character. Underneath that is where you want your “Make it look pretty” stuff. Animators, particle systems, such as dirt being kicked up. Sorry for the late response. I just found this after searching for something else.