Upon Collision enable/diable enemy fire

I’ve been usiong this script below which I found on Unity Answers for my enemies since the start of my most recent project, however, I’ve hit a snag…

I no longer wish to use ‘range’ to know when enemy can attack, but Id rather use a empty game object collider upon collison to tell enemy it can fire…

Below is the code…

var lookAtTarget : Transform;
var speed : int;
var range : int;
var damp : float;
var ENEMY_HEALTH = 30;
var nextFire = 1;
var fireRate = 0.0;
var enemyBullet : Transform;
static var EnemyLives : int = 15;
//var death : Transform ;
//var health : Transform;


function Awake()
{
    if (!lookAtTarget)
    {
        lookAtTarget = GameObject.FindWithTag("Player").transform;
    }
}

function Update() 
{   

    if(lookAtTarget && CanAttackTarget())
    {
        //transform.LookAt(lookAtTarget);
        var rotate = Quaternion.LookRotation(lookAtTarget.transform.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotate, damp); 
        //transform.Translate (0,0,speed*Time.deltaTime);

        if(Time.time >= nextFire)
        {
            nextFire = Time.time + fireRate;
            var bullet = Instantiate(enemyBullet, transform.position, transform.rotation);
           // bullet.rigidbody.velocity = transform.forward * speed;
        }
		
		if(EnemyLives<=0)
		{
		playerScript.playerScore +=10000;
			Destroy(gameObject);

}
}
}

function CanAttackTarget()
{

    //check if in range
    if(Vector3.Distance(transform.position, lookAtTarget.position) > range)
    {
        return false;
    }

    return true;

}

function OnTriggerEnter(otherObject: Collider){
	
	if(otherObject.gameObject.tag == "playerbullet1"){
		EnemyLives --;
		}
		}

I’d assumed that by editing the below

   function CanAttackTarget()
    {
    
        //check if in range
        if(Vector3.Distance(transform.position, lookAtTarget.position) > range)
        {
            return false;
        }

I’d be able to do it, but due to my poor coding ability (been with unity for a couple of weeks or so now) I’m still struggling.

I’d of guessed the way to go would be similar to this:

function CanAttackTarget()
    {
    
        //check if in range
        if(target has collided with playercollison2);
        {
            return false;
        }

but, clearly i dunno… Any ideas?

Thank you.

Physics.OverlapSphere() would work, but would have to be checked continously. It’s often nicer to have indeed a childed empty gameObject with a collider (make sure it’s a trigger!).

To set this up:

  1. remove the entire CanAttackTarget function.
  2. put a variable on the top: canAttack : boolean = false
  3. where it says “&& CanAttackTarget()” replace it with “&& canAttack”
  4. attach a script to the childed empty gameObject that, OnTriggerEnter( player ) sets it to true, and OnTriggerExit sets it to false.

I wouldn’t suggest using a collider, but the best way I know is to use the Physics.OverlapSphere. It will return an array of all the colliders/character controllers touching and inside of the sphere. You can use colliders for this, but if your player uses character controllers, then you will have to use OnControllerColliderHit.