NavMeshAgent to rotate along the terrain
I am struggling with this problem. I have my NavMeshAgent going up and down inclines perfectly fine but I would like them to "rotate" themselves so that I don't see bits of the model clipping through the terrain. My terrain isn't really a terrain, rather it is a bunch of planes placed around the scene. I have my NavMeshAgent so that the parent object have the NavMeshAgent and the model is a child of the agent. On a separate script, I tried doing the following to get the tilt functionality to work;
void Start () { smoothTilt = new Quaternion(); agent = this.GetComponent(); agent.updateRotation = false; }
void Update(){ RaycastHit rHIt; Vector3 ray = transform.TransformDirection(Vector3.down);
if (Physics.Raycast(transform.position, ray, out rHit, groundLayer)) { float GroundDistance = rHit.distance; Quaternion groundTilt = Quaternion.FromToRotation(Vector3.up, rHit.normal); smoothTilt = Quaternion.Slerp(smoothTilt, groundTilt, Time.deltaTime 2.0f); transform.rotation = groundTilt transform.rotation;
Vector3 localPos = transform.localPosition; localPos.y = (transform.localPosition.y - GroundDistance); transform.localPosition = localPos; }
I apply the above script the child object that cointains the model but the object does not rotate up hills. When I apply it to the parent, it does tilt forward but it continues to tilt until half of the model is in the ground (about 90 degress). When the NavMeshAgent begins moving, it spins around like crazy. I understand that I should be using quaternions since I am dealing with rotation, I am just not sure how to do it some that the object doesn't sink into the ground. What should I be doing?
Thank you
Your answer
Follow this Question
Related Questions
How to make the navmesh agent accomplish one task per time? 1 Answer
How to stop NavMeshAgent from using an area? 1 Answer
How to prevent AI from going Backwards 0 Answers
How to change speed for a cloned prefab instead of applying it to all instances of prefab? 1 Answer
Navmesh agent Auto Braking resets transform rotation after reaching destination 0 Answers