- 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.
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;