I need to check if the remaining distance for the navmesh agent to get to the player is below certain length before enabling the navmesh agent, otherwise, I want the navmesh agent to remain disabled and have the enemy go directly towards the player and just break through any obstacles between the enemy and player instead of going around using navmesh. However, to get the Agent.remainingDistance, the agent has to be enabled first, which I don’t want it to be as then the enemy goes around obstacles instead of breaking through.
Here is the code I have so far, but as I get an error at the “if(Agent.remainingDistance < DistanceToTarget + OutOfWayDistance)” statement because the navmesh agent is disabled.
if(Physics2D.Raycast(transform.position, Direction, DistanceToTarget, NavMeshLayer))
{
if(Agent.remainingDistance < DistanceToTarget + OutOfWayDistance)
{
Agent.SetDestination(Target.position);
if(Agent.enabled == false)
{
Agent.enabled = true;
}
if(NavControl == false)
{
NavControl = true;
}
}
else
{
if(NavControl == true)
{
NavControl = false;
}
if(Agent.enabled == true)
{
Agent.enabled = false;
}
}
}
else
{
if(NavControl == true)
{
NavControl = false;
}
if(Agent.enabled == true)
{
Agent.enabled = false;
}
}
}