Advertisement
kissemisse

AI Manager

Mar 14th, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Random = UnityEngine.Random;
  5.  
  6.  
  7. public class AIManager : MonoBehaviour, IAIManager {
  8.  
  9.     [SerializeField] private GameObject[] civilianPrefabs;
  10.  
  11.     private AstarPath astar;
  12.  
  13.     private int activeActors;
  14.  
  15.     private List<Vector3> destinationPoints = new List<Vector3>();
  16.  
  17.     private void Awake() {
  18.         astar = GetComponent<AstarPath>();
  19.     }
  20.  
  21.     public void SpawnActor() {
  22.  
  23.         Vector3 randomSpawn = destinationPoints[Random.Range(0, destinationPoints.Count)];
  24.         GameObject newActor = Instantiate(civilianPrefabs[Random.Range(0,civilianPrefabs.Length)], randomSpawn, Quaternion.identity, transform);
  25.         Civilian newCivilian = newActor.GetComponent<Civilian>();
  26.         newCivilian.SetAstar(this);
  27.        
  28.         newCivilian.SetDestination(destinationPoints[Random.Range(0, destinationPoints.Count)]);
  29.        
  30.     }
  31.  
  32.     public Vector3 GetNewDestination() {
  33.         return destinationPoints[Random.Range(0, destinationPoints.Count)];
  34.     }
  35.  
  36.     public void AddSpawnPoint(Vector3 newSpawnPoint) {
  37.         destinationPoints.Add(newSpawnPoint);
  38.     }
  39.  
  40.     public void RemoveDestinationPoint(Vector3 newDestinationPoint) {
  41.         if(destinationPoints.Contains(newDestinationPoint)) {
  42.             destinationPoints.Remove(newDestinationPoint);
  43.         }
  44.     }
  45.  
  46.     public void UpdateNavMesh(Vector3 position) {
  47.         astar.UpdateGraphs(new Bounds(position, new Vector3(5,5,5)));
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement