Turret won't shoot

I have a turret that looks at the player and is supposed to shoot a ParticleSystem at the player. It looks at the player just fine, but even out of the safe distance I have setup it will not trigger the ParticleSystem. Here is my script:

var findTarget : Transform ;
var lookAtDistance = 20; 
var safeDistance = 15; 
var fireDelay : float;
var prefab : ParticleSystem;
 
function Update(){
 
   var distanceToPlayer = Vector3.Distance(this.transform.position, findTarget .transform.position);
 
   transform.LookAt(findTarget);
 
   if( Input.GetKeyDown( KeyCode.Space ) ) {
      Fire(); 
   }
}     
 
function Fire() {
   Debug.Log("Fire away!");  
   var clone : ParticleSystem;
 
   clone = Instantiate(prefab, transform.position, transform.rotation) as ParticleSystem;
}    

I also need some help adding the ability to take away health points upon contact with the ParticleSystem and player. I’m focusing on the shooting aspect for now but I will need to take damage when that is all ready and done.

//Change this if( Input.GetKeyDown( KeyCode.Space ) ) { Fire(); } //To this if( distanceToPlayer <= safeDistance ) { Fire(); } In your previous question, I put in the Input.GetKeyDown( KeyCode.Space ) for quick-debugging purposes by skipping the check distance.

On top of what Chronos-L said, is your prefab object containing a rigidbody?

the prefab is a particle system, can you make particle systems have rigidbody? after changing it back to if( distanceToPlayer <= safeDistance ) { Fire(); } the particle system still does not fire.

Do you have the "Fire away!" message in your console? Or do you have any error messages?

no that message does not come up, and no errors regarding the turret.

1 Answer

1

This is the one that works in my PC. There are no major difference with yours except that mine is a stripped down version. You can give this a shot.

#pragma strict

var prefab : ParticleSystem;
 
function Update(){     
   if( Input.GetKeyDown( KeyCode.Space ) ) {
      Fire(); 
   }
}     
 
function Fire() {
   Debug.Log("Fire away!");  
   var clone : ParticleSystem;     
   clone = Instantiate(prefab, transform.position, transform.rotation) as ParticleSystem;
}