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;
}
}