Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TowerController : MonoBehaviour
- {
- // Start is called before the first frame update
- public int damage = 1;
- public float speed = 4;
- public float radius = 20;
- public float curTime;
- public int level = 1;
- public int priceUpgrade = 200;
- public int priceSell = 150;
- private GameObject[] enemy;
- private GameObject closestEnemy;
- private GameObject myBase;
- public void LevelUp()
- {
- level++;
- speed = speed - (speed * 0.1f);
- radius = radius + (radius * 0.1f);
- damage = damage + (int)(damage * 0.1f);
- priceUpgrade = priceUpgrade + (int)(priceUpgrade * 0.1f);
- priceSell = priceSell + (int)(priceSell * 0.1f);
- }
- void Start()
- {
- curTime = speed;
- CapsuleCollider m_collider = GetComponent<CapsuleCollider>();
- m_collider.radius = radius;
- myBase = GameObject.FindGameObjectWithTag("Base");
- }
- // Update is called once per frame
- void Update()
- {
- curTime = curTime + Time.deltaTime;
- Collider[] enemyes = Physics.OverlapSphere(transform.position, radius);
- float distance = float.MaxValue;
- closestEnemy = null;
- foreach(Collider obj in enemyes)
- {
- if(obj.gameObject.tag == "Enemy")
- {
- if(Vector3.Distance(myBase.transform.position,
- obj.gameObject.transform.position) < distance)
- {
- distance = Vector3.Distance(myBase.transform.position,
- obj.gameObject.transform.position);
- closestEnemy = obj.gameObject;
- }
- }
- }
- if(closestEnemy != null)
- {
- Attack(closestEnemy);
- }
- }
- void Attack(GameObject enemy)
- {
- if (curTime >= speed)
- {
- Debug.Log("Attack");
- curTime = 0;
- MonsterController monster = enemy.GetComponent<MonsterController>();
- monster.hp -= damage;
- monster.AttackIndicate();
- }
- }
- /*
- void OnTriggerStay(Collider other)
- {
- enemy = GameObject.FindGameObjectsWithTag("Enemy");
- if (other.gameObject.tag == "Enemy")
- {
- //Debug.Log(other.gameObject.name);
- Attack(other.gameObject);
- }
- }*/
- void OnDrawGizmos()
- {
- Gizmos.color = Color.green;
- Gizmos.DrawWireSphere(transform.position, radius);
- }
- /*
- GameObject FindClosesEnemy()
- {
- float distance = Mathf.Infinity;
- Vector3 position = transform.position;
- GameObject closest;
- foreach (GameObject go in enemy)
- {
- Vector3 diff = go.transform.position - position;
- float curDistance = diff.sqrMagnitude;
- if(curDistance < distance)
- {
- closest = go;
- distance = curDistance;
- }
- }
- return closest;
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement