Need help with the radius thing.

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);
		}
	}
}

Im new to unity but here is my suggestion.

Just set s variable on the ontrigger enter.
And then on exit… not sure but prob built in function.

Then fire in the update if variable is set.
Ie if (targetaquired)
{fire at target}

You also will need to add some timeout logic best on time. Delta.
So it don’t fire constantly.

I’ll try that.

I can’t do it :frowning:

Try harder or… quit

You need to maintain a list of active targets in range… something like this should work

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class cannonTurret : MonoBehaviour {

    [SerializeField]
    public GameObject cannonBall;
   public GameObject CurrentTarget;
    public float fireRate = 0.5f;
    public float Range = 75;

     public List<GameObject> Targets;

    void Start()
    {
         Targets = new List<GameObject>();
         InvokeRepeating("Fire", fireRate, fireRate);
     }

    void OnTriggerEnter(Collider collision)
    {
        if(collision.gameObject.tag == "Enemy")
           Targets.Add(collision.gameObject);
    }

   void AssignTarget()
   {
          if(CurrentTargets.count > 0)
          {
             if(CurrentTargets[0] == null)
             {
                  CurrentTargets.RemoveAt(0);
                  AssignTarget();
             }
 
                 if(Vector3.Distance(transform.position, CurrentTargets[0].transform.position) < Range)
                 {
                    CurrentTarget = CurrentTargets[0];
                 }
                 else 
                 {
                    CurrentTarget = null;
                    CurrentTargets.RemoveAt(0);
                    AssignTarget();
                  }
          }
    }   

    void Fire()
    {
      AssignTarget();
      if(CurrentTarget != null)
        Instantiate(cannonBall, transform.position, transform.rotation);
    }

   

 

}