/// <summary> /// Generates a path for an NPC to wander. The path will not leave the defined wander area /// which ensures that the NPC doesn't leave its area and wander to a different plain on the NavMesh /// (given that the NPC is placed near a canyon or similar). /// </summary> /// <param name="wanderArea">The area to generate the path in</param> /// <param name="agent">The agent of the NPC to generate the path for</param> /// <param name="areaMask">The NavMesh area the entity is allowed to move in</param> /// <returns>The next wander path</returns> public NavMeshPath GetNextWanderPath(Area wanderArea, NavMeshAgent agent, int areaMask) { NavMeshPath path = new NavMeshPath(); do { Vector3 wanderPoint = _navMeshMapper.GetMappedRandomPoint(wanderArea, areaMask); agent.CalculatePath(wanderPoint, path); }while (!IsPathInArea(wanderArea, path)); return(path); }
/// <summary> /// Try spawning an NPC. The NPC might not consist in the scene /// (but will be destroyed before it is made visible if it's in the player's sight). /// </summary> protected virtual void TrySpawn() { Vector3 spawnLocation = _navMeshMapper.GetMappedRandomPoint(spawnArea, NavMesh.AllAreas); // setting a random rotation doesn't seem to work with a NavMeshAgent, but since NPCs start walking in a random direction immediately it doesn't really matter EnemyController spawned = Instantiate(toSpawn, spawnLocation, Quaternion.identity, transform); Renderer renderer = spawned.Renderer; // could RenderChecker instead, but no advantages for now if (!renderer.InFrustum(mainCamera)) { renderer.enabled = true; } else { Destroy(spawned.gameObject); } }