Line Renderer not drawing correctly

I’m trying to get effects for my raycast gun working, however for some reason my line renderer is not drawing correctly. The line is a component of the gun itself, and when I shoot the line should draw from the muzzle tip (a separate empty game object). At first the line draws in front of the muzzle (but a lot more than it should) and after about 10 or so shots it just seems to draw in random points on the screen. Use world space is not ticked. Any ideas? Thanks in advance!

EDIT: Also, the muzzle flash (point light) is a little buggy too. Sometimes it stays lit whilst i’m not shooting, and i’m not sure why.

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

public class RaycastGun : GunBaseClass {

    public Light muzzleFlashLight;
    private LineRenderer laserLine;
    public Transform raycastMuzzle;

    private float fireTimer;

    void Start ()
    {
        laserLine = GetComponent<LineRenderer>();
    }

    void Update ()
    {
        if (JoystickFire.instance.Fire)
        {
            Fire ();
        }
        if (fireTimer < fireRate)
            fireTimer += Time.deltaTime;
    }

    private void Fire ()
    {
        if (fireTimer < fireRate)
        {
            muzzleFlashLight.enabled = false;
            return;
        }
      
        StartCoroutine (ShotEffect ());
        RaycastHit hit;
        laserLine.SetPosition (0, raycastMuzzle.position);
        muzzleFlashLight.enabled = true;

        if (Physics.Raycast (raycastMuzzle.position, raycastMuzzle.transform.forward, out hit, gunRange))
        {
            Debug.Log (hit.transform.name + "FoundByRay!");
            laserLine.SetPosition (1, hit.point);
            EnemyBaseClass health = hit.collider.GetComponent<EnemyBaseClass> ();

            if (health != null)
            {
                health.RayDamageReceived (gunDamage);
            }
        }
        else
        {
            laserLine.SetPosition (1, raycastMuzzle.transform.forward * gunRange);
        }

        fireTimer = 0.0f;
    }

    private IEnumerator ShotEffect ()
    {
        laserLine.enabled = true;
        yield return fireTimer;
        laserLine.enabled = false;
    }
}

Tick it. Your code uses “…transform.position” when setting line positions, which is in world space.

Done that and its start position is now correct. Thank you. However, the line still isn’t drawing correctly 100%. A lot of the time it is going at the wrong angle (instead of directly forward from the muzzle). If I hit an enemy though it draws correctly, and as soon as said enemy dies, it draws incorrectly again. For some reason it isn’t following the raycast when it isn’t hitting an object

That’d be this line:

            laserLine.SetPosition (1, raycastMuzzle.transform.forward * gunRange);
should be
            laserLine.SetPosition (1, raycastMuzzle.transform.position + raycastMuzzle.transform.forward * gunRange);

Thank you!