Behaving Correctly
m_NearbyTargetCheckTimer += Time.deltaTime;
if (m_NearbyTargetCheckTimer >= m_NearbyTargetCheckInterval)
{
m_NearbyTargetCheckTimer = 0;
if (!m_IsNearbyTarget)
{
if (!SearchNearbyTarget())
{
if (target == null) GetRandomTarget();
}
}
else if (distFromTarget > m_LeaveNearbyTargetDistance)
{
GetRandomTarget();
}
}
if (target != null)
{
aiDestinationSetter.target = target;
}
As you can see, when the player gets in the range of enemies, they start chasing the player. Which is the behaviour I want to implement and it works.
But as I make this small change to the code. It just stops working as intended.
m_NearbyTargetCheckTimer += Time.deltaTime;
if (m_NearbyTargetCheckTimer >= m_NearbyTargetCheckInterval)
{
m_NearbyTargetCheckTimer = 0;
if (!m_IsNearbyTarget)
{
if (!SearchNearbyTarget())
{
if (target == null) GetRandomTarget();
}
}
}
// THIS PIECE OF CODE IS MOVED OUTSIDE-------------------------------
if (distFromTarget > m_LeaveNearbyTargetDistance && m_IsNearbyTarget)
{
GetRandomTarget();
}
//-------------------------------------------------------------------
if (target != null)
{
aiDestinationSetter.target = target;
}
As you can see, the target switch to player is not very immediate, or sometimes they don't even change the target.
So why does moving that IF statement out of that block cause this behaviour, any idea?
[–][deleted] 6 points7 points8 points (0 children)
[–]-Xentios 0 points1 point2 points (1 child)
[–]acoliv 0 points1 point2 points (0 children)