How do you update NavMesh rotation after stopping distance is reached?

I can’t seem to figure this out. I have the stopping distance set to 1.5 so my AI stops, but when he does he stops rotating towards the target allowing the player to closely sneak behind the enemy without him rotating to the player. This makes it easy for the player to do this with large groups of enemies exploiting the game, due to a ranking system with XP and emblems.

This is written in JS.

I know I can use NavComponent.updateRotation = true but that doesn’t seem to work. I tried a custom rotation, look at function, but that doesn’t seem to work either.

var dir = target.position - transform.position;
  		dir.y = 0; // keep only the horizontal direction
  		var rotation = Quaternion.LookRotation(dir);
  		transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);

This is only called when the player is within the attack distance of about 3. How can I get the enemy to always rotate towards the player/target?

I would post the code here but I’m not aloud to. There is nothing wrong with the code itself it’s just a working rotate towards function I’m looking for that works with NavMesh.

I’m also using a rigidbody on the enemy to give him gravity. Could this be causing an issue? If anyone could point me in the right direction it would be greatly appreciated. I’m only here as a last resort because I can’t seem to get it working myself.

Thanks in advanced.

I had the exact same issue and solved it by rotating the object with the agent if target was in melee range. Below is a snippet of my code. It is written in C#, but should be easy to adapt into JavaScript and similar needs.

    public float meleeRange = 3f;
    public float rotationSpeed = 10f;
    void Update () {		
    	Transform target = null;
    	// Code here that decides what target is
    	if (IsInMeleeRangeOf(target)) {
    private bool IsInMeleeRangeOf (Transform target) {
    	float distance = Vector3.Distance(transform.position, target.position);
    	return distance < meleeRange;
    private void MoveTowards (Transform target) {
    private void RotateTowards (Transform target) {
           Vector3 direction = (target.position - transform.position).normalized;
           Quaternion lookRotation = Quaternion.LookRotation(direction);
           transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);

Update the NavMeshAgents rotation variable:

nMA is my NavMeshAgent.

nMA.updateRotation = true;

if you do same animation set the code in fixed Update its battery : )