Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public class Tower : MonoBehaviour {
- // Public variables
- public GameObject ammoPrefab;
- public float reloadTime = 1f;
- List<GameObject> targetedEnemies;
- // Private variables
- private Transform projectileTarget;
- private GameObject projectile;
- private float nextFireTime;
- private float nextAnimTime;
- void Update() {
- if (projectileTarget) {
- foreach (GameObject target in targetedEnemies){
- Debug.Log("We have target");
- if(Time.time >= nextFireTime){
- Debug.Log("nextFireTime has passed");
- Debug.Log ("Shoot");
- nextFireTime = Time.time + reloadTime;
- projectile = (GameObject)Instantiate (ammoPrefab, transform.position, ammoPrefab.transform.rotation);
- projectile.GetComponent<Projectile> ().target = target.gameObject;
- }
- Health health = target.GetComponent<Health>();
- if(health.currentHealth <= 0){
- targetedEnemies.Remove (target.gameObject);
- }
- }
- }
- }
- void OnTriggerEnter(Collider co) {
- // Was it a Monster? Add to list
- if(co.gameObject.tag == "Enemy")
- {
- Debug.Log("Enemy tag detected");
- nextFireTime = (float)(Time.time+(reloadTime*0.5));
- targetedEnemies.Add(co.gameObject);
- }
- }
- void OnTriggerExit(Collider co) {
- if(co.gameObject.tag == "Enemy")
- {
- targetedEnemies.Remove (co.gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement