Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Minion : MonoBehaviour {
- public float Speed = 15f, Health = 10f, HitTimer = 0f, EnemyMeleeDamage = 2f, EnemyBulletDamage = 1f, EyeTimer, EyeCooldown = 3f, IdleCooldown = 3f, IdleResetTimer;
- public GameObject Controller, Eyes, Body, MinTakeDamage, MinDeath, MinSigh, MinAttack, Left, Target, SpawnVFX, AttackVFX, HitVFX, VFXSpawn, DamageVFXSpawn, GUI, SpawnSFX, DeathModel;
- public bool Touching;
- //Used to calculate the closest enemy
- GameObject FindClosestEnemy()
- {
- List<GameObject> ARValidTargets = GameManager.Instance.ARValidTargets;
- GameObject Closest = null;
- float Distance = Mathf.Infinity;
- Vector3 Position = transform.position;
- foreach (GameObject Targ in ARValidTargets)
- {
- if (Targ == null) continue;
- Vector3 diff = Targ.transform.position - Position;
- float curDistance = diff.sqrMagnitude;
- if (curDistance < Distance)
- {
- Closest = Targ;
- Distance = curDistance;
- }
- }
- return Closest;
- }
- void Start () {
- Eyes = transform.Find ("Head").gameObject.transform.Find ("Eyes").gameObject;
- Body = transform.Find ("Body").gameObject;
- Instantiate (SpawnVFX, VFXSpawn.transform.position, VFXSpawn.transform.rotation);
- Instantiate (SpawnSFX, transform.position, transform.rotation);
- DeathModel = transform.Find ("GoodGuyABreak").gameObject;
- DeathModel.SetActive (false);
- GUI = GameObject.FindGameObjectWithTag ("GUI");
- }
- void Update () {
- //Looks for the left controller until it is found
- if (Controller == null) {
- Controller = GameObject.FindGameObjectWithTag ("Directing");
- }
- //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
- if (Controller != null && Touching == false) {
- if (Controller.GetComponent<DirectMinions> ().directed == true && Controller.GetComponent<DirectMinions> ().controlled == false) {
- Vector3 dir = Controller.GetComponent<DirectMinions> ().directedto - transform.position;
- Vector3 movement = dir.normalized * Speed;
- GetComponent<CharacterController> ().SimpleMove (movement);
- Vector3 dirpos = new Vector3 (Controller.GetComponent<DirectMinions> ().directedto.x, transform.position.y, Controller.GetComponent<DirectMinions> ().directedto.z);
- transform.LookAt (dirpos);
- transform.rotation = Quaternion.Euler (new Vector3 (0, transform.rotation.eulerAngles.y, 0));
- }
- }
- //Used to make the minions always obey gravity
- if (Controller.GetComponent<DirectMinions> ().directed == false && Controller.GetComponent<DirectMinions> ().controlled == false) {
- var slowmove = Vector3.forward * Time.deltaTime;
- GetComponent<CharacterController> ().Move (slowmove);
- }
- //Eye animation
- EyeTimer += 1 * Time.deltaTime;
- if (EyeTimer >= EyeCooldown) {
- Eyes.GetComponent<Animation> ().Stop ();
- Eyes.GetComponent<Animation> ().Play ();
- EyeTimer = 0f;
- }
- //If the minion doesn't have a target, look for a target
- if (Target == null) {
- FindClosestEnemy();
- Target = FindClosestEnemy ();
- }
- //When the mininon dies
- if (Health <= 0) {
- Instantiate (MinDeath, gameObject.transform.position, Quaternion.identity);
- DeathModel.SetActive (true);
- DeathModel.transform.parent = null;
- Destroy (gameObject);
- }
- //If the minion hasn't been controlled in a while, start moving to their closest target
- if (Controller.GetComponent<DirectMinions>().idletimer > IdleCooldown)
- {
- if (Target != null)
- {
- if (GUI.GetComponent<GUIManager> ().started == true) {
- if (Vector3.Distance (MinionRecaller.Instance.transform.position, Target.transform.position) >= (MinionRecaller.Instance.MaxDistance - 1f)) {
- FindClosestEnemy ();
- Target = FindClosestEnemy ();
- }
- if (Controller != null) {
- if (Controller.GetComponent<DirectMinions> ().directed == false && Controller.GetComponent<DirectMinions> ().controlled == false && Touching == false) {
- transform.LookAt (Target.transform);
- transform.rotation = Quaternion.Euler (new Vector3 (0, transform.rotation.eulerAngles.y, 0));
- Vector3 dir = Target.transform.position - transform.position;
- Vector3 movement = dir.normalized * Speed;
- GetComponent<CharacterController> ().SimpleMove (movement);
- }
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment