Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Pathfinding;
- using UnityEngine;
- using Random = UnityEngine.Random;
- public class Civilian : MonoBehaviour, IAI {
- // this script sits on every walking AI character
- // it communicates with an AI manager that gives new destinations
- private AIManager _aiManager;
- private AIPath _agent;
- private Animator _ani;
- [SerializeField] private AnimationClip walkAnimation, idleAnimation;
- [SerializeField] private float speed;
- [SerializeField] private Material[] materials;
- private void Awake() {
- agent = GetComponent<AIPath>();
- //agent.speed = Random.Range(speed -1, speed +1); // random speed
- agent.maxSpeed = speed;
- _ani = transform.GetChild(0).GetComponent<Animator>();
- if(_ani == null) Debug.Log("Animator not found on " + name);
- // randomize material and hat
- Transform materialChild = transform.GetChild(0).GetChild(1);
- Transform hatChild = transform.GetChild(0).GetChild(3);
- if(materialChild) {
- materialChild.GetComponent<SkinnedMeshRenderer>().material =
- materials[Random.Range(0, materials.Length)];
- }
- if(hatChild) {
- hatChild.gameObject.SetActive(Random.value >= .4f); // 40% chance to spawn without hat
- hatChild.GetComponent<SkinnedMeshRenderer>().material = materials[Random.Range(0, materials.Length)];
- }
- }
- private void Update() {
- if(agent.remainingDistance < 1f) {
- SetDestination(aiManager.GetNewDestination());
- _ani.SetBool("Walking", false);
- }
- }
- public void SetDestination(Vector3 targetDestination) {
- agent.destination = targetDestination;
- _ani.SetBool("Walking", true);
- }
- public void SetAstar(AIManager newAIManager) {
- aiManager = newAIManager;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement