Keep AI from flipping?

I have my AI set up with a Navmesh Agent and set up so that if the player is within a certain range, the AI targets the player and walks towards them. When this happens my AI occasionally collides with static objects causing it to tip on it’s x axis. Is there anyway to either reset the rotation of the AI when it flips, or keep the X axis constantly at 0?

I had something previously checking the AI’s rotation, and if it changed to anything but 0, to set the rotation back to 0 but it messed up the entire rotation of the AI causing it to not rotate in the direction it was moving in.

Thanks for any help!

EDIT
I’ve noticed this is more of a problem with the AI flipping in general, not just on the x axis. Is there anyway to normalize the rotation of a Navmesh Agent? To keep it from flipping anytime it collides with another object.

A NavMeshAgent itself alyways stays upright. It will never rotate around X or Z.

I assume you use some mesh or skinned mesh as a character that is parented to your AI.
If you attached a collider to that as well, and it has a rigidbody attached, this might be the problem.
Check all rotation boxes in the rigidbody section, this will prevent it from freely rotating “inside” the AI Object.

If you generally want to restrict the rotation of any transform with a rigidbody inside another, attach a new script with this or include it in any script already attached:

void Update()
{
   transform.localEulerAngles = Vector3.zero;
}

This will always keep the local transform rotation (the rotation inside another transform) at 0,0,0 like you would set in the inspector aswell.

More Info: Unity - Scripting API: Transform

I guess you used transform.rotation in the solution you mentioned in the question, but that uses World Space.

What should I do if I am trying to rotate an object on the x-axis with a navmesh. I need my soldier to rotate 90 degrees on the x so he can lay in the prone, and I need the navmesh so he can move. I actually got him to rotate 90 using the animator. But after that the whole scene breaks.