Advertisement
Mortenssen

FollowPlayer.cs

Sep 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.48 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using Opsive.UltimateCharacterController.Traits;
  6.  
  7. public class FollowPlayer : MonoBehaviour {
  8.  
  9.     private EnemySpawner enemySpawner;
  10.  
  11.     private Transform playerTrans;
  12.  
  13.     private Animator anim;
  14.  
  15.     [SerializeField]
  16.     private bool damaged;
  17.  
  18.     private NavMeshAgent agent;
  19.  
  20.     public GameObject impactPoint;
  21.  
  22.     public Waypoints waypoints;
  23.  
  24.     [SerializeField]
  25.     private Transform nextWaypoint;
  26.  
  27.     public bool movingToWaypoint = false;
  28.  
  29.     public float distanceToPlayer;
  30.  
  31.     public bool playerSeen = false;
  32.  
  33.     private CharacterHealth hp;
  34.     private float previousHP;
  35.  
  36.     private float timeToAttack = 0f;
  37.  
  38.     // Use this for initialization
  39.     void Start ()
  40.     {
  41.         damaged = false;
  42.  
  43.         waypoints = GameObject.Find("Waypoints").GetComponent<Waypoints>();
  44.         enemySpawner = GameObject.Find("WaveSpawner").GetComponent<EnemySpawner>();
  45.  
  46.         hp = GetComponent<CharacterHealth>();
  47.         agent = GetComponent<NavMeshAgent>();
  48.         anim = GetComponent<Animator>();
  49.  
  50.         previousHP = hp.HealthValue;
  51.  
  52.         playerTrans = GameObject.Find("MyNolan").transform;
  53.  
  54.     }
  55.  
  56.     private void Update()
  57.     {
  58.         distanceToPlayer = Vector3.Distance(transform.position, playerTrans.position);
  59.  
  60.         if (previousHP !=  hp.HealthValue)
  61.         {
  62.             Follow();
  63.             OnDamage();
  64.         }
  65.         else
  66.         {
  67.             damaged = false;
  68.         }
  69.  
  70.         if(hp.HealthValue <= 0)
  71.         {
  72.             RemoveEnemyFromList();
  73.         }
  74.  
  75.         if (playerSeen || damaged)
  76.         {
  77.             Follow();
  78.                
  79.         }
  80.         else
  81.         {  
  82.             if(!movingToWaypoint)
  83.             {
  84.                 StartCoroutine("GoToNextWaypoint");
  85.             }
  86.                
  87.         }
  88.     }
  89.  
  90.     void OnDamage()
  91.     {
  92.         damaged = true;
  93.     }
  94.  
  95.     // Update is called once per frame
  96.     void FixedUpdate ()
  97.     {
  98.         timeToAttack -= Time.deltaTime;
  99.     }
  100.  
  101.  
  102.     public void GoToNearestWaypoint()
  103.     {
  104.             for (int i = 0; i < waypoints.waypoints.Length; i++)
  105.             {
  106.                 float distWaypoint = Vector3.Distance(transform.position, waypoints.waypoints[i].transform.position);                
  107.                 if (distWaypoint < 30f)
  108.                 {
  109.                     nextWaypoint = waypoints.waypoints[i].transform;
  110.                     agent.SetDestination(nextWaypoint.position);
  111.                     movingToWaypoint = true;
  112.  
  113.                     if (agent.velocity.magnitude > 3f)
  114.                     {
  115.                         anim.SetBool("Run", true);
  116.                         anim.SetBool("Attack", false);
  117.                     }
  118.                     else
  119.                     {
  120.                         anim.SetBool("Run", false);
  121.                         anim.SetBool("Attack", false);
  122.                     }
  123.                 }
  124.             }  
  125.     }
  126.  
  127.     public IEnumerator GoToNextWaypoint()
  128.     {
  129.         movingToWaypoint = true;
  130.         agent.SetDestination(transform.position);
  131.         anim.SetBool("Following", false);
  132.         anim.SetBool("Run", false);
  133.         yield return new WaitForSeconds(2.5f);
  134.         nextWaypoint = waypoints.waypoints[Random.Range(0, waypoints.waypoints.Length)].transform;
  135.         agent.SetDestination(nextWaypoint.position);
  136.  
  137.         if (agent.velocity.magnitude > 3f)
  138.         {
  139.             anim.SetBool("Following", true);
  140.             anim.SetBool("Run", true);
  141.             anim.SetBool("Attack", false);
  142.         }
  143.         else
  144.         {
  145.             anim.SetBool("Following", true);
  146.             anim.SetBool("Run", false);
  147.             anim.SetBool("Attack", false);
  148.         }
  149.     }
  150.  
  151.     private void OnTriggerEnter(Collider other)
  152.     {
  153.         if(other.gameObject.tag == "Waypoint")
  154.         {
  155.             StartCoroutine("GoToNextWaypoint");
  156.         }
  157.     }
  158.  
  159.     /*public void Idle()
  160.     {
  161.         playerSeen = false;
  162.         anim.SetBool("Following", false);
  163.         anim.SetBool("Run", false);
  164.         agent.SetDestination(transform.position);
  165.     }*/
  166.  
  167.     public void Follow()
  168.     {
  169.  
  170.         movingToWaypoint = false;
  171.  
  172.         anim.SetBool("Following", true);
  173.         anim.SetBool("Attack", false);
  174.  
  175.         if (distanceToPlayer <= 2.5f)
  176.         {
  177.             anim.SetBool("Run", false);
  178.             anim.SetBool("Attack", true);
  179.             if (timeToAttack <= 0f)
  180.                 TryAttack();
  181.         }
  182.  
  183.         agent.SetDestination(new Vector3(playerTrans.position.x, playerTrans.position.y, playerTrans.position.z));
  184.        
  185.         if (agent.velocity.magnitude > 3f)
  186.         {
  187.             anim.SetBool("Run", true);
  188.         }
  189.  
  190.         if (distanceToPlayer > 30f)
  191.         {
  192.             playerSeen = false;
  193.         }
  194.         if (distanceToPlayer > 40)
  195.         {
  196.             damaged = false;
  197.         }
  198.     }
  199.  
  200.     void TryAttack()
  201.     {
  202.         Attack();
  203.         timeToAttack = 1f;
  204.     }
  205.  
  206.     void Attack()
  207.     {
  208.         transform.LookAt(playerTrans);
  209.         impactPoint.SetActive(true);
  210.         StartCoroutine("DeactivateImpact", .5f);
  211.  
  212.     }
  213.  
  214.     public IEnumerator DeactivateImpact(float seconds)
  215.     {
  216.         yield return new WaitForSeconds(seconds);
  217.         impactPoint.SetActive(false);        
  218.     }
  219.  
  220.     public void RemoveEnemyFromList()
  221.     {
  222.         enemySpawner.SendMessage("RemoveEnemy", this.gameObject);
  223.         Destroy(gameObject);
  224.     }
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement