'How to fire bullets in Unity 2D?

I'm currently working on a platform 2D game. So far made like 2 levels and I'm currently working on a boss level. I used a free model blue Sci-Fi type of gun, and I don't know how to make it fire bullets. The gun is attached to the player,its always pointing in the direction of the mouse,and once the raycast detects its colliding with the boss layer,it draws a red line (on top of the already drawn blue line). I hope i explained it so you can understand what i mean. Here's the code.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class WeaponScript : MonoBehaviour
{

    public float fireRate = 0;
    public float Damage = 5;
    public LayerMask whatToHit;

    private float timeToFire = 0;
    Transform firePoint;
    // Start is called before the first frame update
    void Awake()
    {
        firePoint = transform.FindChild("FirePoint");
        if(firePoint == null){
            Debug.LogError("No FirePoint. What?");
        }
    }

    public float offset;

    void Update()
    {
        Shoot();
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);

        if(fireRate == 0) {
            if(Input.GetMouseButtonDown(0)) {
                Shoot();
            }
        }

        else {
            if(Input.GetMouseButton(0) && Time.time > timeToFire) {
                timeToFire = Time.time + 1/fireRate;
                Shoot();
            }
        }

    }
    void Shoot() {
        Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
        Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
        RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
        Debug.DrawLine(firePointPosition, (mousePosition-firePointPosition)*100, Color.blue);
        if(hit.collider != null){
            Debug.DrawLine(firePointPosition, hit.point, Color.red);
            
        }
    }
}


Solution 1:[1]

Just Instantiate a prefab. Then destroy it after a bit of delay, or after it has traveled a set distance. You can create a prefab by making an object (for example a cube) in the scene, adding scripts to it and drag it into the project window (where your files are), then drag the prefab from there into the slot on your script

// add this variable to your Script, this is where you drag your prefab in the inspector
public GameObject bulletPrefab;

// Put this in your shoot function
Instantiate(bulletPrefab, GUNTRANSFORM, GUNTRANSFORM.rotation, transform);

On the Prefab you place a MonoBehaviour like this:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;
public class Bullet : MonoBehaviour 
{
    public float speed = 20;

    void Update() 
    {
        transform.Translate((transform.forward * speed * Time.deltaTime));
    }
}

References: Instantiate, Transform.Translate, Time.deltaTime YouTube tutorial

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1