Using 2D Raycast with LineRendere in unity

You can use the only 2D Raycast without Linerendere in your game.

Because of Linerendere not necessary for 2D Raycast to work, I only use it to show the invisible ray.

Ask or Request for anything you want in the comments below 😀

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayCast : MonoBehaviour
{

    public float range = 8f;
    Ray shootRay = new Ray();
    RaycastHit2D shootHit;
    int shootableMask;
    LineRenderer gunLine;
    public bool shooting = false;

    void Awake()
    {
        shootableMask = LayerMask.GetMask("Shootable");
        gunLine = GetComponent<LineRenderer>();
    }

    void Update()
    {
        if(shooting)
            Shoot();
    }


    void Shoot()
    {
        gunLine.SetPosition(0, transform.position);

        shootRay.origin = transform.position;
        shootRay.direction = transform.right; // raycast direction 


        shootHit = Physics2D.Raycast(shootRay.origin, shootRay.direction, range, shootableMask);

        if (shootHit)
        {
            //what will happen if the raycast shoot a abject with "Shootable" Mask
            gunLine.SetPosition(1, shootHit.point);
        }
        else
        {
            //what will happen if the raycast dont shoot any abject with "Shootable" Mask
            gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
        }
    }
}

FOR MORE