LineRenderer Raycast Gun Bug
I have an issue with the LineRenderer component and when I try to implement it into script so that it acts as a visible ray.
Heres the code of the gun that fires the ray:
using UnityEngine; using System.Collections;
[RequireComponent (typeof (AudioSource))] public class dev_ray_shoot : MonoBehaviour {
 public enum GunType {Semi,Burst,Auto};
 public GunType gunType;
 public float rpm;
 
 //Components
 public Transform rayExit;
 private LineRenderer tracer;
 
 //System
 private float secondsBetweenShots;
 private float nextPossibleShootTime;
 
 void Start() {
     secondsBetweenShots = 60/rpm;
     if (GetComponent<LineRenderer>()){
         tracer = GetComponent<LineRenderer>();
     }
 }
 
 public void Shoot() {
     if (CanShoot()){
         Ray ray = new Ray(rayExit.position,rayExit.forward);
         RaycastHit hit;
         
         float rayDistance = 20;
         
         if (Physics.Raycast(ray,out hit, rayDistance = 20)){
             rayDistance = hit.distance;
         }
         
         nextPossibleShootTime = Time.time + secondsBetweenShots;
         
         AudioSource audio = GetComponent<AudioSource> ();
         audio.Play(); 
         if (tracer){
             StartCoroutine("RenderTracer", ray.direction * rayDistance);
         }
         Debug.DrawRay (ray.origin,ray.direction * rayDistance, Color.red, 1);
     }
 }
 
 public void Shoot_Automatic () {
     if (gunType == GunType.Auto) {
         Shoot ();
     }
 }
 
 private bool CanShoot() {
     bool canShoot = true;
     
     if (Time.time < nextPossibleShootTime) {
         canShoot = false;
     }
     
     return canShoot;
 }
 IEnumerator RenderTracer(Vector3 hitPoint){
     tracer.enabled = true;
     tracer.SetPosition(0,rayExit.localPosition);
     tracer.SetPosition(1,rayExit.localPosition + hitPoint);
     yield return null;
     tracer.enabled = false;
 }
}
The linerenderer component which is attatched to the same gameobject as this script does not fully follow the ray the way its supposed to. So can someone please help? I have a picture of it:
(The yellow line is the linerenderer and the red line is the debug.drawray) for extra information, the LineRenderer deforms when I fire in different directions, it changes its length each time.
Please help, thank you.
P.S. Im using Unity 5
Answer by doublemax · Sep 09, 2016 at 04:10 PM
Instead of:
 tracer.SetPosition(1,rayExit.localPosition + hitPoint);
Try:
 tracer.SetPosition(1,rayExit.position + ray.direction * hit.distance);
If I just change that it will say the variables are missing/unkown in the current context, due to the those things being declared only in the Update, not in the IEnumerator RenderTracer.
If I add them at the top (after the "$$anonymous$$onobehaviour {" ) where most variables are then it accepts the script but the linerenderer just disappears somewhere unknown.
So it doesn't fully work.
For point 0 replace localPosition with position, too.
 tracer.SetPosition(0,rayExit.position);
Nope, I set both to have position ins$$anonymous$$d of localPosition and replaced the HitPoint stuff, also added Ray ray, RaycastHit hit at the top of the code with the other variables, but like last time it just disappears.
- for extra information, the LineRenderer deforms when I fire in different directions, it changes its length each time. 
Your answer
 
 
             Follow this Question
Related Questions
Having an issue Making a second ray cast with a new angle 1 Answer
Strange Raycast problem 0 Answers
Scripting Errors 1 Answer
(Raycasts) C# Why am I shooting myself? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                