C
C#2y ago
Kaelen

❔ Nav Mesh Agent doesnt avoid obstacles

Hey, here there's a cross which indicates where my bot have to go I set a nav mesh obstacle on the barrier. Why doesnt my agent avoid obstacles ??
4 Replies
HimmDawg
HimmDawg2y ago
The information given to us isn't really much to work with. Can you go further into detail and show the relevant code? Also, that sounds like Unity. It might be worth a shot to ask in the unity discord $unity
Kaelen
KaelenOP2y ago
I search a random point on the nav mesh :
public class RandomPointOnNavMesh : MonoBehaviour
{

public static bool RandomPoint(Vector3 center, float range, out Vector3 result)
{
for (int i = 0; i < 30; i++)
{
Vector3 randomPoint = center + Random.insideUnitSphere * range;
NavMeshHit hit;
if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas))
{
result = hit.position;
return true;
}
}
result = Vector3.zero;
return false;
}

}
public class RandomPointOnNavMesh : MonoBehaviour
{

public static bool RandomPoint(Vector3 center, float range, out Vector3 result)
{
for (int i = 0; i < 30; i++)
{
Vector3 randomPoint = center + Random.insideUnitSphere * range;
NavMeshHit hit;
if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas))
{
result = hit.position;
return true;
}
}
result = Vector3.zero;
return false;
}

}
Then I set the direction of my agent to this point :
private void newDir(zombieStateManager zombie){
if (RandomPointOnNavMesh.RandomPoint(zombie.transform.position, range, out point))
{
Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f);
}
Debug.Log(point);
zombie.pathFind.MoveTo(point);
Debug.Log("Changement de position :" + point);
}
private void newDir(zombieStateManager zombie){
if (RandomPointOnNavMesh.RandomPoint(zombie.transform.position, range, out point))
{
Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f);
}
Debug.Log(point);
zombie.pathFind.MoveTo(point);
Debug.Log("Changement de position :" + point);
}
Here is my pathfind class :
public class pathFinding : MonoBehaviour
{

private NavMeshAgent navMeshAgent;
// Start is called before the first frame update
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}

public void MoveTo(Vector3 dir){
navMeshAgent.SetDestination(dir);
}

}
public class pathFinding : MonoBehaviour
{

private NavMeshAgent navMeshAgent;
// Start is called before the first frame update
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}

public void MoveTo(Vector3 dir){
navMeshAgent.SetDestination(dir);
}

}
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server