Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Pathfinding;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- [RequireComponent(typeof(Rigidbody2D))]
- [RequireComponent(typeof(Seeker))]
- public class EnemyAI : MonoBehaviour {
- //What to chase
- public Transform target;
- //How many times each second we will update our path
- public float updateRate = 2f;
- //Caching
- private Seeker seeker;
- private Rigidbody2D rb;
- //The calculated path
- public Path path;
- //The AI's speed per second
- //custom variables TODO: 8byte?
- public float speed = 7f;
- public float speedMultiplyer = 1.1f;
- private bool _FlapWing = false;
- private float flapStrength = 1.5f;
- private Animator _Anim;
- public ForceMode2D fMode;
- [HideInInspector]
- public bool pathIsEnded = false;
- //The max distance from the AI to a waypoint for it to continue to the next waypoint
- public float nextWaypointDistance = 1; //TODO: 8byte? changed this from 3
- //The waypoint we are currently moving towards
- private int currentWaypoint = 0;
- private bool searchingForPlayer = false;
- private void Awake()
- {
- _Anim = GetComponent<Animator>();
- }
- void Start()
- {
- seeker = GetComponent<Seeker>();
- rb = GetComponent<Rigidbody2D>();
- if(target == null)
- {
- if(!searchingForPlayer)
- {
- searchingForPlayer = true;
- StartCoroutine(SearchForPlayer());
- }
- return;
- }
- //Start a new path to the target position, return the result to the OnPathComplete method
- seeker.StartPath(transform.position, target.position, OnPathComplete);
- StartCoroutine (UpdatePath());
- }
- IEnumerator SearchForPlayer()
- {
- GameObject sResult = GameObject.FindGameObjectWithTag("Player");
- if(sResult == null)
- {
- yield return new WaitForSeconds(0.5f);
- StartCoroutine(SearchForPlayer());
- }
- else
- {
- target = sResult.transform;
- searchingForPlayer = false;
- StartCoroutine(UpdatePath());
- yield return false; //2018 Unity yeld requirement
- }
- }
- IEnumerator UpdatePath()
- {
- if(target == null)
- {
- if(!searchingForPlayer)
- {
- searchingForPlayer = true;
- StartCoroutine(SearchForPlayer());
- }
- yield return false; //2018 Unity yeld requirement
- ;
- }
- seeker.StartPath(transform.position, target.position, OnPathComplete);
- //Start a new path to the target position, return the result to the OnPathComplete method
- yield return new WaitForSeconds (1f / updateRate);
- StartCoroutine (UpdatePath());
- }
- public void OnPathComplete(Path p)
- {
- //Debug.Log("We got a path. Did it have an error? " + p.error);
- if(!p.error)
- {
- path = p;
- currentWaypoint = 0;
- }
- }
- //IEnumerator method to wait x seconds instad of updating every frame
- IEnumerator HeightControl() //check for vulture height
- {
- //Flapping-Flying
- //_FlapWing = false;
- if(target.transform.position.y > seeker.transform.position.y)
- {
- if(rb.velocity.y < 6)
- {
- //_FlapWing = true;
- //FindObjectOfType<AudioManager>().Play("FlapWing");
- yield return new WaitForSeconds(0.5f); //time to wait
- rb.AddForce(Vector2.up * flapStrength, ForceMode2D.Impulse);
- }
- else if(target.transform.position.y < seeker.transform.position.y)
- {
- //rb.AddForce(Vector2.right * speed * speedMultiplyer, ForceMode2D.Force);
- }
- }
- yield break; //always use yield break when ending an enumerator
- }
- void FixedUpdate()
- {
- if(target == null)
- {
- if(!searchingForPlayer)
- {
- searchingForPlayer = true;
- StartCoroutine(SearchForPlayer());
- }
- return;
- }
- if(path == null)
- {
- return;
- }
- if(currentWaypoint >= path.vectorPath.Count)
- {
- if(pathIsEnded)
- {
- return;
- }
- Debug.Log("End of path reached.");
- pathIsEnded = true;
- return;
- }
- pathIsEnded = false;
- //Direction to the next waypoint
- 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
- dir = (dir * speed) * Time.fixedDeltaTime; //time bound speed
- //Move the AI
- rb.AddForce(dir, fMode);
- float dist = (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]));
- if(dist < nextWaypointDistance)
- {
- currentWaypoint++;
- return;
- }
- HeightControl();
- SendAnimInfo();
- }
- void SendAnimInfo()
- {
- _Anim.SetBool("FlapWing", _FlapWing);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement