l3enjamin

CyberSiege VR - Minion.cs

May 8th, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.41 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Minion : MonoBehaviour {
  6.  
  7.     public float Speed = 15f, Health = 10f, HitTimer = 0f, EnemyMeleeDamage = 2f, EnemyBulletDamage = 1f, EyeTimer, EyeCooldown = 3f, IdleCooldown = 3f, IdleResetTimer;
  8.     public GameObject Controller, Eyes, Body, MinTakeDamage, MinDeath, MinSigh, MinAttack, Left, Target, SpawnVFX, AttackVFX, HitVFX, VFXSpawn, DamageVFXSpawn, GUI, SpawnSFX, DeathModel;
  9.     public bool Touching;
  10.     //Used to calculate the closest enemy
  11.     GameObject FindClosestEnemy()
  12.     {
  13.         List<GameObject> ARValidTargets = GameManager.Instance.ARValidTargets;
  14.         GameObject Closest = null;
  15.         float Distance = Mathf.Infinity;
  16.         Vector3 Position = transform.position;
  17.         foreach (GameObject Targ in ARValidTargets)
  18.         {
  19.             if (Targ == null) continue;
  20.             Vector3 diff = Targ.transform.position - Position;
  21.             float curDistance = diff.sqrMagnitude;
  22.             if (curDistance < Distance)
  23.             {
  24.                 Closest = Targ;
  25.                 Distance = curDistance;
  26.             }
  27.         }
  28.         return Closest;
  29.     }
  30.  
  31.     void Start () {
  32.         Eyes = transform.Find ("Head").gameObject.transform.Find ("Eyes").gameObject;
  33.         Body = transform.Find ("Body").gameObject;
  34.         Instantiate (SpawnVFX, VFXSpawn.transform.position, VFXSpawn.transform.rotation);
  35.         Instantiate (SpawnSFX, transform.position, transform.rotation);
  36.         DeathModel = transform.Find ("GoodGuyABreak").gameObject;
  37.         DeathModel.SetActive (false);
  38.         GUI = GameObject.FindGameObjectWithTag ("GUI");
  39.     }
  40.  
  41.     void Update () {
  42.         //Looks for the left controller until it is found
  43.         if (Controller == null) {
  44.             Controller = GameObject.FindGameObjectWithTag ("Directing");
  45.         }
  46.         //If the controller is found and the minion isn't touching, and if the player has directed the minions, move and look at the spot
  47.         if (Controller != null && Touching == false) {
  48.             if (Controller.GetComponent<DirectMinions> ().directed == true && Controller.GetComponent<DirectMinions> ().controlled == false) {
  49.                 Vector3 dir = Controller.GetComponent<DirectMinions> ().directedto - transform.position;
  50.                 Vector3 movement = dir.normalized * Speed;
  51.                 GetComponent<CharacterController> ().SimpleMove (movement);
  52.                 Vector3 dirpos = new Vector3 (Controller.GetComponent<DirectMinions> ().directedto.x, transform.position.y, Controller.GetComponent<DirectMinions> ().directedto.z);
  53.                 transform.LookAt (dirpos);
  54.                 transform.rotation = Quaternion.Euler (new Vector3 (0, transform.rotation.eulerAngles.y, 0));
  55.             }
  56.         }
  57.         //Used to make the minions always obey gravity
  58.         if (Controller.GetComponent<DirectMinions> ().directed == false && Controller.GetComponent<DirectMinions> ().controlled == false) {
  59.             var slowmove = Vector3.forward * Time.deltaTime;
  60.             GetComponent<CharacterController> ().Move (slowmove);
  61.         }
  62.         //Eye animation
  63.         EyeTimer += 1 * Time.deltaTime;
  64.         if (EyeTimer >= EyeCooldown) {
  65.             Eyes.GetComponent<Animation> ().Stop ();
  66.             Eyes.GetComponent<Animation> ().Play ();
  67.             EyeTimer = 0f;
  68.         }
  69.         //If the minion doesn't have a target, look for a target
  70.         if (Target == null) {
  71.             FindClosestEnemy();
  72.             Target = FindClosestEnemy ();
  73.         }
  74.         //When the mininon dies
  75.         if (Health <= 0) {
  76.             Instantiate (MinDeath, gameObject.transform.position, Quaternion.identity);
  77.             DeathModel.SetActive (true);
  78.             DeathModel.transform.parent = null;
  79.             Destroy (gameObject);
  80.         }
  81.         //If the minion hasn't been controlled in a while, start moving to their closest target
  82.         if (Controller.GetComponent<DirectMinions>().idletimer > IdleCooldown)
  83.         {      
  84.             if (Target != null)
  85.             {
  86.                 if (GUI.GetComponent<GUIManager> ().started == true) {
  87.                     if (Vector3.Distance (MinionRecaller.Instance.transform.position, Target.transform.position) >= (MinionRecaller.Instance.MaxDistance - 1f)) {
  88.                         FindClosestEnemy ();
  89.                         Target = FindClosestEnemy ();
  90.                     }
  91.                     if (Controller != null) {
  92.                         if (Controller.GetComponent<DirectMinions> ().directed == false && Controller.GetComponent<DirectMinions> ().controlled == false && Touching == false) {
  93.                             transform.LookAt (Target.transform);
  94.                             transform.rotation = Quaternion.Euler (new Vector3 (0, transform.rotation.eulerAngles.y, 0));
  95.                             Vector3 dir = Target.transform.position - transform.position;
  96.                             Vector3 movement = dir.normalized * Speed;
  97.                             GetComponent<CharacterController> ().SimpleMove (movement);
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.     }
  104. }
Add Comment
Please, Sign In to add comment