Enemy Shoots Self instead of Player

I’m trying to fix this script so that the Enemy stops shooting backwards into itself (or is walking into its bullets without ignore collision working.

I’m not sure why this is not shooting at the player…

var LookAtTarget:Transform; 
var damp = 6.0; 
var bullitPrefab:Transform; 
var savedTime=0; 

function Update () 
{ 
     if(LookAtTarget) 
   { 
        var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); 
       
       transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 
       
       var seconds : int = Time.time; 
       var oddeven = (seconds % 2); 
       
       if(oddeven) 
       { 
	     Debug.Log ("shooting"); 
             Shoot(seconds); 
       } 
   } 
} 

function Shoot(seconds) 
{      
     if(seconds!=savedTime) 
    { 
	//transform.LookAt(Target); 
	var bullit = Instantiate(bullitPrefab, transform.LookAt(LookAtTarget);
Physics.IgnoreCollision(bullitPrefab.collider, transform.root.collider);			
  bullit.rigidbody.AddForce(transform.forward  * 10); 
//var bullit = Instantiate(bullitPrefab, transform.position, transform.rotation);
//Physics.IgnoreCollision(bullitPrefab.collider, transform.root.collider);			
 //  bullit.rigidbody.AddForce(transform.forward  * 10); 
   
    	     Debug.Log ("shot"); 
   savedTime=seconds; 
   }
   }

Well a few possibilities but are you sure the bullets are not simply colliding with each other. Since it seems like you are making 2 bullets each time you shoot.

I agree with aiursrage2k. Your code for “seconds” seems odd.

var seconds : int = Time.time; 
       var oddeven = (seconds % 2); 
        
       if(oddeven)

This is going to pause for a second, then fire 50-100 times during the next second, then pause again. Note that Debug.Log omits repeating messages, so you’ll probably only see “shooting” once. You can get around that by using this Debug.Log statement:

 Debug.Log ("shooting          and random=" + Random.Range(0,1000000));

If you only want to allow shooting once every 2 seconds, try something like this:

var fl_last_shot : float;

function Update() {
    if (fl_last_shot + 2 < Time.time) {
        Shoot(2);
        fl_last_shot = Time.time;
    }
}

If that does nothing, you might also instantiate the bullet a couple meters in front of the enemy.