Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- using Opsive.UltimateCharacterController.Traits;
- public class FollowPlayer : MonoBehaviour {
- private EnemySpawner enemySpawner;
- private Transform playerTrans;
- private Animator anim;
- [SerializeField]
- private bool damaged;
- private NavMeshAgent agent;
- public GameObject impactPoint;
- public Waypoints waypoints;
- [SerializeField]
- private Transform nextWaypoint;
- public bool movingToWaypoint = false;
- public float distanceToPlayer;
- public bool playerSeen = false;
- private CharacterHealth hp;
- private float previousHP;
- private float timeToAttack = 0f;
- // Use this for initialization
- void Start ()
- {
- damaged = false;
- waypoints = GameObject.Find("Waypoints").GetComponent<Waypoints>();
- enemySpawner = GameObject.Find("WaveSpawner").GetComponent<EnemySpawner>();
- hp = GetComponent<CharacterHealth>();
- agent = GetComponent<NavMeshAgent>();
- anim = GetComponent<Animator>();
- previousHP = hp.HealthValue;
- playerTrans = GameObject.Find("MyNolan").transform;
- }
- private void Update()
- {
- distanceToPlayer = Vector3.Distance(transform.position, playerTrans.position);
- if (previousHP != hp.HealthValue)
- {
- Follow();
- OnDamage();
- }
- else
- {
- damaged = false;
- }
- if(hp.HealthValue <= 0)
- {
- RemoveEnemyFromList();
- }
- if (playerSeen || damaged)
- {
- Follow();
- }
- else
- {
- if(!movingToWaypoint)
- {
- StartCoroutine("GoToNextWaypoint");
- }
- }
- }
- void OnDamage()
- {
- damaged = true;
- }
- // Update is called once per frame
- void FixedUpdate ()
- {
- timeToAttack -= Time.deltaTime;
- }
- public void GoToNearestWaypoint()
- {
- for (int i = 0; i < waypoints.waypoints.Length; i++)
- {
- float distWaypoint = Vector3.Distance(transform.position, waypoints.waypoints[i].transform.position);
- if (distWaypoint < 30f)
- {
- nextWaypoint = waypoints.waypoints[i].transform;
- agent.SetDestination(nextWaypoint.position);
- movingToWaypoint = true;
- if (agent.velocity.magnitude > 3f)
- {
- anim.SetBool("Run", true);
- anim.SetBool("Attack", false);
- }
- else
- {
- anim.SetBool("Run", false);
- anim.SetBool("Attack", false);
- }
- }
- }
- }
- public IEnumerator GoToNextWaypoint()
- {
- movingToWaypoint = true;
- agent.SetDestination(transform.position);
- anim.SetBool("Following", false);
- anim.SetBool("Run", false);
- yield return new WaitForSeconds(2.5f);
- nextWaypoint = waypoints.waypoints[Random.Range(0, waypoints.waypoints.Length)].transform;
- agent.SetDestination(nextWaypoint.position);
- if (agent.velocity.magnitude > 3f)
- {
- anim.SetBool("Following", true);
- anim.SetBool("Run", true);
- anim.SetBool("Attack", false);
- }
- else
- {
- anim.SetBool("Following", true);
- anim.SetBool("Run", false);
- anim.SetBool("Attack", false);
- }
- }
- private void OnTriggerEnter(Collider other)
- {
- if(other.gameObject.tag == "Waypoint")
- {
- StartCoroutine("GoToNextWaypoint");
- }
- }
- /*public void Idle()
- {
- playerSeen = false;
- anim.SetBool("Following", false);
- anim.SetBool("Run", false);
- agent.SetDestination(transform.position);
- }*/
- public void Follow()
- {
- movingToWaypoint = false;
- anim.SetBool("Following", true);
- anim.SetBool("Attack", false);
- if (distanceToPlayer <= 2.5f)
- {
- anim.SetBool("Run", false);
- anim.SetBool("Attack", true);
- if (timeToAttack <= 0f)
- TryAttack();
- }
- agent.SetDestination(new Vector3(playerTrans.position.x, playerTrans.position.y, playerTrans.position.z));
- if (agent.velocity.magnitude > 3f)
- {
- anim.SetBool("Run", true);
- }
- if (distanceToPlayer > 30f)
- {
- playerSeen = false;
- }
- if (distanceToPlayer > 40)
- {
- damaged = false;
- }
- }
- void TryAttack()
- {
- Attack();
- timeToAttack = 1f;
- }
- void Attack()
- {
- transform.LookAt(playerTrans);
- impactPoint.SetActive(true);
- StartCoroutine("DeactivateImpact", .5f);
- }
- public IEnumerator DeactivateImpact(float seconds)
- {
- yield return new WaitForSeconds(seconds);
- impactPoint.SetActive(false);
- }
- public void RemoveEnemyFromList()
- {
- enemySpawner.SendMessage("RemoveEnemy", this.gameObject);
- Destroy(gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement