Question by
ItzChris92 · May 07, 2017 at 12:02 PM ·
raycastraycastinglinerendererline rendererline drawing
Raycast line renderer not drawing correctly
As the title says. When I first press play the line seems to draw correctly, and when I move about sometimes the line will be going off in a random direction. Note that this is not always happening. However, from looking at Debug.Log the Raycast is travelling directly forward and hitting the correct points so it isn't a huge issue, however I will be using the line renderer for extra effects. Any help would be much appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RaycastGun : MonoBehaviour {
public Light muzzleFlashLight;
private LineRenderer laserLine;
public Transform raycastMuzzle;
float fireTimer;
void Start ()
{
laserLine = GetComponent<LineRenderer>();
}
void Update ()
{
if (JoystickFire.instance.Fire)
{
Fire (); //Execute Fire function if right stick is "Down"
}
if (fireTimer < gameObject.GetComponent<GunStats>().fireRate)
fireTimer += Time.deltaTime; //Add into time counter
}
private void Fire()
{
if (fireTimer < gameObject.GetComponent<GunStats>().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, gameObject.GetComponent<GunStats>().gunRange))
{
Debug.Log (hit.transform.name + "FoundByRay!");
laserLine.SetPosition (1, hit.point);
EnemyDamage health = hit.collider.GetComponent<EnemyDamage> ();
if (health != null)
{
health.DamageReceived (gameObject.GetComponent<GunStats>().gunDamage);
}
}
else
{
laserLine.SetPosition (1, raycastMuzzle.transform.forward * gameObject.GetComponent<GunStats>().gunRange);
}
fireTimer = 0.0f; //Reset fireTimer
}
private IEnumerator ShotEffect()
{
laserLine.enabled = true;
yield return fireTimer;
laserLine.enabled = false;
}
}
Comment
I am thinking it could possibly be my Nav $$anonymous$$esh interfering here. Still not sure how to fix the problem though
Your answer
Follow this Question
Related Questions
Raycast Troubleshooting 1 Answer
issue with LineRender start from 0 0 Answers
line render some lines not visible 2 Answers
Line Renderer on top of Mesh 1 Answer