Advertisement
sphinx2001

TowerController

Feb 2nd, 2021
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TowerController : MonoBehaviour
  6. {
  7.     // Start is called before the first frame update
  8.     public int damage = 1;
  9.     public float speed = 4;
  10.     public float radius = 20;
  11.     public float curTime;
  12.     public int level = 1;
  13.     public int priceUpgrade = 200;
  14.     public int priceSell = 150;
  15.  
  16.     private GameObject[] enemy;
  17.     private GameObject closestEnemy;
  18.     private GameObject myBase;
  19.     public void LevelUp()
  20.     {
  21.         level++;
  22.         speed = speed - (speed * 0.1f);
  23.         radius = radius + (radius * 0.1f);
  24.         damage = damage + (int)(damage * 0.1f);
  25.         priceUpgrade = priceUpgrade + (int)(priceUpgrade * 0.1f);
  26.         priceSell = priceSell + (int)(priceSell * 0.1f);
  27.     }
  28.     void Start()
  29.     {
  30.         curTime = speed;
  31.         CapsuleCollider m_collider = GetComponent<CapsuleCollider>();
  32.         m_collider.radius = radius;
  33.         myBase = GameObject.FindGameObjectWithTag("Base");
  34.     }
  35.  
  36.     // Update is called once per frame
  37.     void Update()
  38.     {
  39.         curTime = curTime + Time.deltaTime;
  40.         Collider[] enemyes = Physics.OverlapSphere(transform.position, radius);        
  41.         float distance = float.MaxValue;
  42.         closestEnemy = null;
  43.         foreach(Collider obj in enemyes)
  44.         {
  45.             if(obj.gameObject.tag == "Enemy")
  46.             {
  47.                 if(Vector3.Distance(myBase.transform.position,
  48.                     obj.gameObject.transform.position) < distance)
  49.                 {
  50.                     distance = Vector3.Distance(myBase.transform.position,
  51.                     obj.gameObject.transform.position);
  52.                     closestEnemy = obj.gameObject;
  53.                 }
  54.             }
  55.         }
  56.         if(closestEnemy != null)
  57.         {
  58.             Attack(closestEnemy);
  59.         }
  60.     }
  61.  
  62.     void Attack(GameObject enemy)
  63.     {
  64.         if (curTime >= speed)
  65.         {
  66.             Debug.Log("Attack");
  67.             curTime = 0;
  68.             MonsterController monster = enemy.GetComponent<MonsterController>();
  69.             monster.hp -= damage;
  70.             monster.AttackIndicate();
  71.         }
  72.     }
  73.     /*
  74.     void OnTriggerStay(Collider other)
  75.     {
  76.         enemy = GameObject.FindGameObjectsWithTag("Enemy");
  77.  
  78.         if (other.gameObject.tag == "Enemy")
  79.         {
  80.             //Debug.Log(other.gameObject.name);
  81.             Attack(other.gameObject);
  82.         }
  83.     }*/
  84.  
  85.     void OnDrawGizmos()
  86.     {
  87.         Gizmos.color = Color.green;
  88.         Gizmos.DrawWireSphere(transform.position, radius);
  89.  
  90.     }
  91.    
  92.     /*
  93.     GameObject FindClosesEnemy()
  94.     {
  95.         float distance = Mathf.Infinity;
  96.         Vector3 position = transform.position;
  97.         GameObject closest;
  98.         foreach (GameObject go in enemy)
  99.         {
  100.             Vector3 diff = go.transform.position - position;
  101.             float curDistance = diff.sqrMagnitude;
  102.             if(curDistance < distance)
  103.             {
  104.                 closest = go;
  105.                 distance = curDistance;
  106.             }
  107.         }
  108.         return closest;
  109.     }*/
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement