ltrout

EnemyMovement

Dec 16th, 2015
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using PlayerInfo;
  4. using Shell;
  5.  
  6. namespace EnemyInfo
  7. {
  8.     public class EnemyMovement : MonoBehaviour
  9.     {
  10.         public Transform ObjectToFollow;
  11.         public Transform target;
  12.         public Transform PlayerTank;
  13.         public Transform EnemyTank;
  14.         public int AttackRange = 35;
  15.         public bool inRange;
  16.         public int TooClose = 10;
  17.  
  18.         Transform player;
  19.         NavMeshAgent agent;
  20.         static PlayerHealth pHealth;
  21.         static EnemyHealth eHealth;
  22.         Rigidbody Rigidbody;
  23.  
  24.         void Awake ()
  25.         {
  26.             player = GameObject.FindWithTag ("Player").transform;
  27.             pHealth = player.GetComponent<PlayerHealth> ();
  28.             eHealth = GetComponent<EnemyHealth> ();
  29.             Rigidbody = GetComponent<Rigidbody> ();
  30.         }
  31.  
  32.         void Update ()
  33.         {
  34.             NavMeshAgent agent = GetComponent<NavMeshAgent> ();
  35.  
  36.             transform.LookAt (target);
  37.  
  38.             if (pHealth.CurrentHealth > 0 && eHealth.CurrentHealth > 0)
  39.             {
  40.                 agent.SetDestination (player.position);
  41.             }
  42.             else
  43.             {
  44.                 agent.enabled = false;
  45.             }
  46.  
  47.             if (Vector3.Distance (PlayerTank.position, EnemyTank.position) < AttackRange) {
  48.                 if (Vector3.Distance (PlayerTank.position, EnemyTank.position) > TooClose) {
  49.                     inRange = true;
  50.                 }
  51.             }
  52.             else if (Vector3.Distance (PlayerTank.position, EnemyTank.position) > AttackRange)
  53.             {
  54.                 inRange = false;
  55.             }
  56.  
  57.         }
  58.  
  59.         private void OnEnable ()
  60.         {
  61.             // When the tank is turned on, make sure it's not kinematic.
  62.             Rigidbody.isKinematic = false;
  63.         }
  64.  
  65.  
  66.         private void OnDisable ()
  67.         {
  68.             // When the tank is turned off, set it to kinematic so it stops moving.
  69.             Rigidbody.isKinematic = true;
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment