Raycast Enemy AI shooting script

Okay, so these are the stats. I’m trying to make a third person shooter. I have created two prefab bullets. One is shot by my player. Another is used by my enemies. The idea is for them (the enemies) to move around (chase after me), and shoot at me. I don’t want them to fill me with bullets right when the game starts and to continue firing without pause. There should be a fire rate limiting that. I had a script that worked badly. Now I have another one but the enemies just stand around doing nothing at all. Can you please help me to fix it? Thanks! Here’s the script:

var fwd : Vector3;    //raycast
 var distance : float = 10.0f;                    //distance
 var timer : float;
 var amountOfBullets : int = 0;
 var amountOfBullets_reset;
 var bullets_Array : GameObject[];
 var hit : UnityEngine.Collision; 

 function Start() {
  fwd = transform.TransformDirection(Vector3.forward);
  function amountofBullets_reset(){
      bullets_Array = GameObject.FindGameObjectsWithTag("Bullet");
      amountOfBullets = bullets_Array.Length;
 function Update () 
      if (Physics.Raycast (transform.position, fwd, distance)) {    //raycast forward and see...
      if(hit.transform.gameObject.name == "MainCharacter"){        //whether it is a player...
          Fire();                        //if this runs, then the player is in the sight of the enemy! 
 function Fire () { 
 //inside of firing function:
  if(timer > Random.Range(3,5)&&amountOfBullets<5){
      timer = 0.0f;
  timer += 1 * Time.deltaTime;

@ZefanS @LucasMars You guys have been very wise and helped me greatly in the past. Do you think you could please help me now too? Thanks!

Your problem might be in your fwd vector: you just set it once at startup, which will initialize it to a forward vector at that point in time… instead of using your fwd vector, you might just use transform.forward which will always point to the direction your transform is looking at… :slight_smile: