Advertisement
RoshHoul

Untitled

Jan 27th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.AI;
  6.  
  7. public class EnemyAgent : MonoBehaviour {
  8.  
  9.     GameObject[] waypoints;
  10.     List<GameObject> visitedWaypoints = new List<GameObject>();
  11.  
  12.     public AgentState currentState, prevState;
  13.     GameObject target;
  14.  
  15.     public GameObject player;
  16.     NavMeshAgent agent;
  17.  
  18.     Animator anim;
  19.  
  20.     private void Start()
  21.     {
  22.         anim = GetComponent<Animator>();
  23.  
  24.         if (waypoints == null)
  25.         {
  26.             waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
  27.         }
  28.  
  29.         if (agent == null)
  30.         {
  31.             agent = GetComponent<NavMeshAgent>();
  32.         }
  33.  
  34.         currentState = AgentState.Patrolling;
  35.         prevState = AgentState.Idle;
  36.  
  37.         GameObject tempTarget = TakeRandomWaypoint();
  38.  
  39.         if (tempTarget != null && IsValidWaypoint(tempTarget))
  40.         {
  41.             target = tempTarget;
  42.         }
  43.  
  44.     }
  45.  
  46.     // Update is called once per frame
  47.     void Update () {
  48.         if (prevState != currentState)
  49.         {
  50.             //currentState = prevState;
  51.             if (currentState == AgentState.Patrolling)
  52.             {
  53.                 Debug.Log("Target is: " + target.name);
  54.                 agent.speed = 3.5f;
  55.                 agent.SetDestination(target.transform.position);
  56.                 anim.SetBool("startWalking", true);
  57.                 //Debug.Log("Dist is " + Vector3.Distance(agent.destination, agent.transform.position));
  58.                 if (IsPathReached())
  59.                 {
  60.                     Debug.Log(target.name + " reached");
  61.                     prevState = currentState;
  62.                     currentState = AgentState.Idle;
  63.                     target = null;
  64.                 }
  65.  
  66.             }
  67.             else if (currentState == AgentState.Idle)
  68.             {
  69.                 //execute idle animations
  70.                 anim.SetBool("startWalking", false);
  71.                 anim.SetInteger("idleRand", RandomAnimation());
  72.                 StartCoroutine(WaitForAnimation());
  73.  
  74.                 target = TakeRandomWaypoint();
  75.                 if (target != null && IsValidWaypoint(target))
  76.                 {
  77.                     prevState = currentState;
  78.                     currentState = AgentState.Patrolling;
  79.  
  80.                 }
  81.                 Debug.Log("druga animaciq beibe");
  82.             }
  83.             if (IsPlayerInReach())
  84.             {
  85.                 //    Debug.Log("TAPANAR GUBISH");
  86.                 target = player;
  87.             }
  88.         }
  89.     }
  90.  
  91.     private IEnumerator WaitForAnimation()
  92.     {
  93.         agent.isStopped = true;
  94.         yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length + anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
  95.         agent.isStopped = false;
  96.     }
  97.  
  98.     private int RandomAnimation()
  99.     {
  100.         int i = UnityEngine.Random.Range(0, 100);
  101.  
  102.         return i % 2;
  103.     }
  104.  
  105.     private GameObject TakeRandomWaypoint()
  106.     {
  107.         UnityEngine.Random rand = new UnityEngine.Random();
  108.         GameObject target = waypoints[UnityEngine.Random.Range(0, waypoints.Length)];
  109.  
  110.         return target;
  111.     }
  112.  
  113.     private bool IsValidWaypoint(GameObject waypoint)
  114.     {
  115.         if (visitedWaypoints.Contains(waypoint))
  116.         {
  117.             return false;
  118.         }
  119.         return true;
  120.     }
  121.  
  122.     private bool IsPathReached()
  123.     {
  124.         if (Vector3.Distance(agent.destination, agent.transform.position) <= agent.stoppingDistance)
  125.         {
  126.             if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)
  127.             {
  128.                 return true;
  129.             }
  130.         }
  131.  
  132.         return false;
  133.     }
  134.  
  135.     private bool IsPlayerInReach()
  136.     {
  137.         Vector3 dir = player.transform.position - transform.position;
  138.         if (!Physics.Raycast(transform.position, dir, 25,  1 << LayerMask.NameToLayer("Walls"))) {
  139.             if (Physics.Raycast(transform.position, dir, 15,  1 << LayerMask.NameToLayer("Player")))
  140.             {
  141.                 return true;
  142.             }
  143.            
  144.             return false;
  145.         }
  146.  
  147.         return false;
  148.     }
  149. }
  150.  
  151. public enum AgentState
  152. {
  153.     Idle,
  154.     Patrolling
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement