- Home /
Y-axis rotation changing on its own? (NavMesh agent + terrain alignment)
I have create a small triangular prism-like object that moves around a terrain using a NavMesh agent that I located on an empty parent object. Meanwhile, I rotate the objects x- and z- axis, so that the prism is aligned to the ground as the parent climbs up and down the hills of the terrain. This is the code that does the alignment, using a raycast onto the terrain:
RaycastHit hit1;
Physics.Raycast(transform.position, -transform.up, out hit1, 0.7f);
Vector3 newUp = hit1.normal;
transform.up = newUp;
transform.eulerAngles = new Vector3(transform.eulerAngles.x,
0,
transform.eulerAngles.z);
As you can see, I turn the y rotation to 0, so that the parent's NavMesh is the one that controls the y rotation according to its navigation movement. I.e. if it moves south, the rotation will point south, as the NavMesh agent always does. However, when I run the game, the y rotation of the object does get a value (coming out of nowhere, apparently), that equals the parents y rotation but with the opposite sign. So that, if the NavMesh produces a y rotation of 120º, the y rotation of the child object will be -120º, thus keeping the overall y rotation at 0º.
Any ideas for why this should be happening would highly appreciated, because I really haven't got a clue.
Your answer
Follow this Question
Related Questions
Need some help with Navmesh agents backing off from player / circling player please. 0 Answers
How to control a NavMeshAgent's rotation ? 1 Answer
Looking forward while walking with navMeshAgent 1 Answer
Split Rotation and Moving of a NavMeshAgent 1 Answer
NavMeshAgent resume original position and facing direction C# 0 Answers