I am trying to create a tower defence game.
My problem is, I extended my sphere collider in a cube and it’s firing. But, only once and I need it to continue firing as long as the enemy cube is in the radius.
For the cube who is firing
using UnityEngine;
using System.Collections;
public class cannonTurret : MonoBehaviour {
[SerializeField]
public GameObject cannonBall;
public float fireRate = 0.5f;
private float nextFire = 0.0f;
void OnTriggerEnter(Collider collision)
{
Debug.Log("Got Hit");
if(collision.gameObject.tag == "Enemy" Time.deltaTime> nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(cannonBall, transform.position, transform.rotation);
}
}
//void Fire()
//{
// Instantiate(cannonBall, transform.position, transform.rotation);
//}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
script that chases the cube
using UnityEngine;
using System.Collections;
public class enemyChaser : MonoBehaviour {
private Transform targetEnemy;
[SerializeField]
public float projectileSpeed;
// Use this for initialization
void Start () {
projectileSpeed = 1.0f;
targetEnemy = GameObject.FindGameObjectWithTag("Enemy").transform;
}
// Update is called once per frame
void Update () {
transform.LookAt(targetEnemy);
this.rigidbody.velocity = transform.forward * projectileSpeed;
}
void OnTriggerStay(Collider col)
{
if(col.gameObject.tag == "Enemy")
{
Debug.Log("HIT");
Destroy(this.gameObject);
}
}
}
