title: How to fire bullets in Unity 2D?
body: <p>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.</p>
  <pre class="lang-cs prettyprint-override"><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(&quot;FirePoint&quot;);
          if(firePoint == null){
              Debug.LogError(&quot;No FirePoint. What?&quot;);
          }
      }
  
      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) &amp;&amp; Time.time &gt; 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);
              
          }
      }
  }
  </code></pre>