Advertisement
kissemisse

Civilian

Mar 14th, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. using System;
  2. using Pathfinding;
  3. using UnityEngine;
  4. using Random = UnityEngine.Random;
  5.  
  6. public class Civilian : MonoBehaviour, IAI {
  7.  
  8.     // this script sits on every walking AI character
  9.     // it communicates with an AI manager that gives new destinations
  10.    
  11.     private AIManager _aiManager;
  12.     private AIPath _agent;
  13.     private Animator _ani;
  14.  
  15.     [SerializeField] private AnimationClip walkAnimation, idleAnimation;
  16.  
  17.     [SerializeField] private float speed;
  18.  
  19.     [SerializeField] private Material[] materials;
  20.  
  21.     private void Awake() {
  22.         agent = GetComponent<AIPath>();
  23.         //agent.speed = Random.Range(speed -1, speed +1); // random speed
  24.         agent.maxSpeed = speed;
  25.        
  26.         _ani = transform.GetChild(0).GetComponent<Animator>();
  27.         if(_ani == null) Debug.Log("Animator not found on " + name);
  28.  
  29.         // randomize material and hat
  30.         Transform materialChild = transform.GetChild(0).GetChild(1);
  31.         Transform hatChild = transform.GetChild(0).GetChild(3);
  32.  
  33.         if(materialChild) {
  34.             materialChild.GetComponent<SkinnedMeshRenderer>().material =
  35.                 materials[Random.Range(0, materials.Length)];
  36.         }
  37.  
  38.         if(hatChild) {
  39.             hatChild.gameObject.SetActive(Random.value >= .4f); // 40% chance to spawn without hat
  40.             hatChild.GetComponent<SkinnedMeshRenderer>().material = materials[Random.Range(0, materials.Length)];
  41.         }
  42.     }
  43.  
  44.     private void Update() {
  45.         if(agent.remainingDistance < 1f) {
  46.             SetDestination(aiManager.GetNewDestination());
  47.             _ani.SetBool("Walking", false);
  48.         }
  49.     }
  50.  
  51.     public void SetDestination(Vector3 targetDestination) {
  52.         agent.destination = targetDestination;
  53.         _ani.SetBool("Walking", true);
  54.     }
  55.  
  56.     public void SetAstar(AIManager newAIManager) {
  57.         aiManager = newAIManager;
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement