Enemy shooting at player

Hi guys! So, I’m working on a game level for college that’s due this Friday, it’s a runner-type level with a helicopter chasing and shooting the player, however I can’t get the shooting script to work. I’ll post my script here, mostly taken from this video: How to Make Your Enemies Fire Projectiles - Unity 2D Platformer Tutorial - Part 23 - YouTube . If anyone can help me with this, please do so, this is urgent :

public float playerRange;
public GameObject player;
public Transform launchPoint;
public float bulletSpeed = 10;
public Rigidbody bullet;
void Start()
{
    player = GameObject.FindGameObjectWithTag("Player");
}
void Fire()
{
    Rigidbody bulletClone = Instantiate(bullet, launchPoint.position, launchPoint.rotation);
    bulletClone.velocity = transform.forward * bulletSpeed;
}
void Update()
{
    Debug.DrawLine(new Vector3(transform.position.x - playerRange, transform.position.y, transform.position.z), new Vector3(transform.position.x + playerRange, transform.position.y, transform.position.z));
    Fire();
}

Thanks, anyone who reads this! :smiley:

EDIT: Changed some code, here it is now still not working:

void Start()
{
    player = GameObject.FindGameObjectWithTag("Player");
}
void Fire()
{
  Instantiate(bullet, launchPoint.position, launchPoint.rotation);
  print("It should be firing at this point");
}
// Update is called once per frame
void Update()
{
    Debug.DrawLine(new Vector3(transform.position.x - playerRange, transform.position.y, transform.position.z), new Vector3(transform.position.x + playerRange, transform.position.y, transform.position.z));
    if (transform.localScale.x < 0 && player.transform.position.x > transform.position.x && player.transform.position.x < transform.position.x + playerRange)
    {
        Fire();
    }
}

AND here’s the bullet code I added, damage commented out for now:

void Start () {
	
}

// Update is called once per frame
void Update() {
    GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);
}
private void OnTriggerEnter2D(Collider2D other)
{
    if(other.tag == "Player")
    {
        //PlayerCK.hurtPlayer(damage);
    }
}

(Global variables removed for formatting)

Why make bulletClone and bullet Rigidbodies? isn’t it easier to make them GameObjects and prefab rigidbodies withing them?

Try :

public GameObject bullet;

 void Fire()
 {
     Gameobject bulletClone = Instantiate(bullet, launchPoint.position, launchPoint.rotation);
     bulletClone.GetComponent<Rigidbody>().velocity = transform.forward * bulletSpeed;
 }

Let me know if this worked.