/// <summary> /// Adds a new agent to the scene /// </summary> public NavAgent Spawn(Vector3 position) { int index = Random.Range(0, agentsToSpawn.Count); NavAgent agent = Poolable.TryGetPoolable <NavAgent>( agentsToSpawn[index].gameObject); agent.transform.localScale = Vector3.one * world.scale; agent.transform.rotation = world.transform.rotation; agent.transform.position = position; agent.gameObject.SetActive(true); NavAgentArchetype archetype = agent.configuration.archetype; int archetypeIndex = agentManager.archetypes.IndexOf(archetype); NativeArray <AgentBehaviors> behaviors = agentManager.agentBehaviors[archetypeIndex]; // set the agent's navigation behaviors AgentBehaviors flags = AgentBehaviors.Active | AgentBehaviors.Queueing | AgentBehaviors.Avoiding | AgentBehaviors.Seeking; NativeSlice <AgentBehaviors> slice = behaviors.Slice(agent.index, 1); SetAgentBehaviors activation = new SetAgentBehaviors() { flags = flags, behaviors = slice }; activation.Schedule(1, 1).Complete(); return(agent); }
/// <summary> /// Adds multiple agents randomly in the scene /// </summary> public void SpawnAgents(int count) { Vector3 position = Vector3.zero; Vector3Int grid = Vector3Int.zero; Vector2 center = new Vector2( world.size.x / 2, world.size.z / 2 ); for (int i = 0; i < count; i++) { // choose a random (x,z) position inside a circle Vector2 randomXZ = Random.insideUnitCircle; randomXZ *= world.size.x / 2; randomXZ += center; // update the agents grid position and // convert the grid position to world space grid.x = (int)randomXZ.x; grid.z = (int)randomXZ.y; grid.y = heightMap.Read(grid.x, grid.z) + 1; position = world.GridToScene(grid); // spawn the new enemy agent int index = Random.Range(0, agentsToSpawn.Count); NavAgent agent = Poolable.TryGetPoolable <NavAgent>( agentsToSpawn[index].gameObject); agent.transform.localScale = Vector3.one * world.scale; agent.transform.rotation = world.transform.rotation; agent.transform.position = position; agent.gameObject.SetActive(true); // enable the agents navigation behaviors to SEEK, QUEUE, and AVOID NavAgentArchetype archetype = agent.configuration.archetype; int archetypeIndex = agentManager.archetypes.IndexOf(archetype); NativeArray <AgentBehaviors> behaviors = agentManager.agentBehaviors[archetypeIndex]; AgentBehaviors flags = AgentBehaviors.Active | AgentBehaviors.Queueing | AgentBehaviors.Avoiding | AgentBehaviors.Seeking; NativeSlice <AgentBehaviors> slice = behaviors.Slice(agent.index, 1); SetAgentBehaviors activation = new SetAgentBehaviors() { flags = flags, behaviors = slice }; activation.Schedule(1, 1).Complete(); } }
/// <summary> /// Removes an agent from the scene /// </summary> public void Remove(Enemy enemy) { NavAgent agent = enemy.GetComponent <NavAgent>(); Poolable.TryPool(agent.gameObject); // set the agent's navigation behaviors to NONE NavAgentArchetype archetype = agent.configuration.archetype; int archetypeIndex = agentManager.archetypes.IndexOf(archetype); NativeArray <AgentBehaviors> behaviors = agentManager.agentBehaviors[archetypeIndex]; AgentBehaviors flags = AgentBehaviors.None; NativeSlice <AgentBehaviors> slice = behaviors.Slice(agent.index, 1); SetAgentBehaviors activation = new SetAgentBehaviors() { flags = flags, behaviors = slice }; activation.Schedule(1, 1).Complete(); }