Use an else clause

It feels like it’d be nicer not to have two if statements in EngageTarget.

Instead of

if (distanceToTarget >= navMeshAgent.stoppingDistance)
{
  ChaseTarget();
}

if (distanceToTarget <= navMeshAgent.stoppingDistance)
{
  AttackTarget();
}

Write:

if (distanceToTarget >= navMeshAgent.stoppingDistance)
{
  ChaseTarget();
}
else
{
  AttackTarget();
}

Note that the two examples are not exactly equivalent because the original code has an overlap when distanceToTarget == navMeshAgent.stoppingDistance but I’m not sure if it’s useful.

1 Like

You are correct. In the original code both methods will execute, and in your suggested code it will only execute ChaseTarget(). I guess the implementation depends on what you want to achieve.

Privacy & Terms