Advertisement
Guest User

Untitled

a guest
May 27th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using Pathfinding;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. [RequireComponent(typeof(Rigidbody2D))]
  7. [RequireComponent(typeof(Seeker))]
  8. public class EnemyAI : MonoBehaviour {
  9.  
  10.     //What to chase
  11.     public Transform target;
  12.  
  13.     //How many times each second we will update our path
  14.     public float updateRate = 2f;
  15.  
  16.     //Caching
  17.     private Seeker seeker;
  18.     private Rigidbody2D rb;
  19.  
  20.     //The calculated path
  21.     public Path path;
  22.  
  23.     //The AI's speed per second
  24.     //custom variables TODO: 8byte?
  25.     public float speed = 7f;
  26.     public float speedMultiplyer = 1.1f;
  27.     private bool _FlapWing = false;
  28.     private float flapStrength = 1.5f;
  29.     private Animator _Anim;
  30.  
  31.     public ForceMode2D fMode;
  32.  
  33.     [HideInInspector]
  34.     public bool pathIsEnded = false;
  35.  
  36.     //The max distance from the AI to a waypoint for it to continue to the next waypoint
  37.     public float nextWaypointDistance = 1; //TODO: 8byte? changed this from 3
  38.  
  39.     //The waypoint we are currently moving towards
  40.     private int currentWaypoint = 0;
  41.  
  42.     private bool searchingForPlayer = false;
  43.  
  44.     private void Awake()
  45.     {
  46.         _Anim = GetComponent<Animator>();
  47.     }
  48.  
  49.     void Start()
  50.     {
  51.         seeker = GetComponent<Seeker>();
  52.         rb = GetComponent<Rigidbody2D>();
  53.  
  54.         if(target == null)
  55.         {
  56.             if(!searchingForPlayer)
  57.             {
  58.                 searchingForPlayer = true;
  59.                 StartCoroutine(SearchForPlayer());
  60.             }
  61.             return;
  62.         }
  63.  
  64.         //Start a new path to the target position, return the result to the OnPathComplete method
  65.         seeker.StartPath(transform.position, target.position, OnPathComplete);
  66.  
  67.         StartCoroutine (UpdatePath());
  68.     }
  69.  
  70.     IEnumerator SearchForPlayer()
  71.     {
  72.         GameObject sResult = GameObject.FindGameObjectWithTag("Player");
  73.         if(sResult == null)
  74.         {
  75.             yield return new WaitForSeconds(0.5f);
  76.             StartCoroutine(SearchForPlayer());
  77.         }
  78.         else
  79.         {
  80.             target = sResult.transform;
  81.             searchingForPlayer = false;
  82.             StartCoroutine(UpdatePath());
  83.             yield return false; //2018 Unity yeld requirement
  84.         }
  85.     }
  86.  
  87.  
  88.     IEnumerator UpdatePath()
  89.     {
  90.         if(target == null)
  91.         {
  92.             if(!searchingForPlayer)
  93.             {
  94.                 searchingForPlayer = true;
  95.                 StartCoroutine(SearchForPlayer());
  96.             }
  97.             yield return false; //2018 Unity yeld requirement
  98.             ;
  99.         }
  100.  
  101.  
  102.         seeker.StartPath(transform.position, target.position, OnPathComplete);
  103.         //Start a new path to the target position, return the result to the OnPathComplete method
  104.  
  105.  
  106.  
  107.         yield return new WaitForSeconds (1f / updateRate);
  108.         StartCoroutine (UpdatePath());
  109.     }
  110.  
  111.     public void OnPathComplete(Path p)
  112.     {
  113.         //Debug.Log("We got a path. Did it have an error? " + p.error);
  114.         if(!p.error)
  115.         {
  116.             path = p;
  117.             currentWaypoint = 0;
  118.         }
  119.     }
  120.  
  121.  
  122.     //IEnumerator method to wait x seconds instad of updating every frame
  123.     IEnumerator HeightControl()  //check for vulture height
  124.     {
  125.         //Flapping-Flying
  126.         //_FlapWing = false;
  127.         if(target.transform.position.y > seeker.transform.position.y)
  128.         {
  129.             if(rb.velocity.y < 6)
  130.             {
  131.                 //_FlapWing = true;
  132.                 //FindObjectOfType<AudioManager>().Play("FlapWing");
  133.                 yield return new WaitForSeconds(0.5f); //time to wait
  134.                 rb.AddForce(Vector2.up * flapStrength, ForceMode2D.Impulse);
  135.                
  136.             }
  137.             else if(target.transform.position.y < seeker.transform.position.y)
  138.             {
  139.                 //rb.AddForce(Vector2.right * speed * speedMultiplyer, ForceMode2D.Force);
  140.             }
  141.         }
  142.  
  143.  
  144.  
  145.         yield break; //always use yield break when ending an enumerator
  146.     }
  147.  
  148.  
  149.     void FixedUpdate()
  150.     {
  151.         if(target == null)
  152.         {
  153.             if(!searchingForPlayer)
  154.             {
  155.                 searchingForPlayer = true;
  156.                 StartCoroutine(SearchForPlayer());
  157.             }
  158.             return;
  159.         }
  160.  
  161.         if(path == null)
  162.         {
  163.             return;
  164.         }
  165.  
  166.         if(currentWaypoint >= path.vectorPath.Count)
  167.         {
  168.             if(pathIsEnded)
  169.             {
  170.                 return;
  171.             }
  172.  
  173.             Debug.Log("End of path reached.");
  174.             pathIsEnded = true;
  175.             return;
  176.         }
  177.         pathIsEnded = false;
  178.  
  179.         //Direction to the next waypoint
  180.         Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized; //.normalized takes the magnitude out of a vector, essentialy taking the "speed out of it" and just assigning a direction
  181.         dir = (dir * speed) * Time.fixedDeltaTime; //time bound speed
  182.  
  183.         //Move the AI
  184.         rb.AddForce(dir, fMode);
  185.  
  186.         float dist = (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]));
  187.         if(dist < nextWaypointDistance)
  188.         {
  189.             currentWaypoint++;
  190.             return;
  191.         }
  192.  
  193.         HeightControl();
  194.         SendAnimInfo();
  195.  
  196.     }
  197.  
  198.     void SendAnimInfo()
  199.     {
  200.         _Anim.SetBool("FlapWing", _FlapWing);
  201.     }
  202.  
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement