An agent is shaking when reaches the target

  • A* version: 5.3.8
  • Unity version: 6.2

I have an agent which follows the player if he attacks him. When the agent reaches the player, it starts shaking. The player and the agent are on different physics layers.

https://streamable.com/lkhksw

Here is the code block that controls the attacking:

case EnemyState.Attacking:
                    _richAI.destination = revengeTarget.transform.position;
                    var distanceThreshold = _halfDepth + revengeTarget.GetComponent<Collider>().bounds.extents.z+3;
                    var distanceToTarget = Vector3.Distance(revengeTarget.transform.position, transform.position);
                    if (distanceToTarget <= distanceThreshold)
                    {
                        if(!_richAI.isStopped)
                            _richAI.isStopped = true;
                        _attackProgress+=Time.deltaTime;
                        
                        Vector3 directionToTarget=revengeTarget.transform.position - transform.position;
                        float rotationStep = Time.deltaTime;
                        Vector3 newDirection=Vector3.RotateTowards(transform.forward, directionToTarget, rotationStep,0);
                        transform.rotation=Quaternion.LookRotation(newDirection);
                        
                        if (_attackProgress > attackCooldown)
                        {
                            Physics.Raycast(transform.position+_halfHeight, transform.forward, out RaycastHit hit, distanceThreshold+0.1f);
                            if (hit.collider != null)
                            {
                                if (hit.collider.gameObject == revengeTarget)
                                {
                                    PlayerController pc = revengeTarget.GetComponent<PlayerController>();
                                    if (pc != null)
                                    {
                                        // if (pc.Damage(damage) <= 0)
                                        // {
                                        //     revengeTarget = null;
                                        // }
                                    }
                                }
                            }

                            _richAI.isStopped = false;
                            _attackProgress = 0;
                        }
                        StopWalking();
                    }
                    else
                    {
                        if(distanceToTarget!=0)
                            Debug.Log(distanceToTarget);
                        _attackProgress += Time.deltaTime;
                        if (_attackProgress > attackCooldown)
                        {
                            _richAI.isStopped = false;
                            _attackProgress = 0;
                        }
                    }
                    break;

Is this being called on Update by chance? I’m wondering if the isStopped is being toggled on and off every frame and causing this.

Yes, but I debugged it and it isn’t being toggled. I even disabled “_richAI.isStopped = true” and it’s still happening.

Found the cause: transform.rotation=Quaternion.LookRotation(newDirection);
I need the agent to turn towards the target even when he reached it, how can I do that?

Have you turned updateRotation to false? With it off, the line you found as problematic should work- as in, it’ll keep the agent from trying to restore control of it’s rotation automatically.