0

For enemies, I set a random position on the nav mesh every x seconds, so that the enemies walk around randomly. Here my scripts:

Random point (from the unity docs)

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

My random movement script:

IEnumerator Walking ()
{
    if (!isFollowingPlayer && isServer) {
        Debug.LogError ("-- Start Walking --");
        agent.speed = 2;
        Vector3 randomDirection;
        bool blocked = RandomPoint (agent.transform.position, walkRadius, out randomDirection);
        NavMeshHit hit;
        blocked = NavMesh.Raycast (enemy.transform.position, randomDirection, out hit, NavMesh.AllAreas);
        if (!blocked) {
            geileMethodeZumZeichnen (agent);
            target = hit.position;
            agent.SetDestination (hit.position);
            geileMethodeZumZeichnen (agent);
            Debug.LogWarning ("Go to " + hit.position);
        } 
        if (blocked) {
            Debug.LogWarning ("-- BLOCKED --");
            yield return new WaitForEndOfFrame ();
        } else {
            Debug.LogWarning ("-- WAIT --");
            yield return new WaitForSeconds (5f);
        }
        StartCoroutine ("Walking");
    }
}

The random walking generally works. However when a nav mesh agent is close to the border of the nav mesh, it appears that he "breaks out" and is then stuck to his position. He only "jitters around" outside the nav mesh. (red lines on the image : path)

NavMesh

Looks like they try to reach their position but can not enter the nav mesh again. I don't want them to leave in first place :)

Mirco
  • 2,940
  • 5
  • 34
  • 57
  • Hello, fgirst of all, I don't know what the "geileMethodeZumZeichnen (agent)" function does. Anyway, the only problem I see is that you are calling the walk coroutine inside the coroutine (i believe that the coroutine stops when the main one ends). One last thing, I guess that you are drawing debug lines to see the agents targets. If not, you should do that. Thanks. – Cabrra Jul 03 '16 at 12:06
  • Sorry, yeah, thats exactly what the method does. My variable "blocked" means - "the way is blocked", so I call it again to calculate another point – Mirco Jul 03 '16 at 12:08
  • This may be a stupid question. But why don't you call walk in Update()? It may be less optimal and wasting processor cycles. But I think that when yiu call a coroutine inside a corotine, the second coroutine stops when the first one ends. (unles you have an infinite loop in the first one). I don't know if you understand this... :( sorry – Cabrra Jul 03 '16 at 12:13

1 Answers1

0

Okay, this was pretty easy in the end: I did not set up the physics correctly. The enemies really 'felt' into the edge and got stuck there because of physics. Working like a charm now.

Mirco
  • 2,940
  • 5
  • 34
  • 57