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