Hi, I have the problem that I found out few other people have had before me too but nobody seems to come up with actual solution or at least in every case those have been very different from each others.
So the thing is that I’m using LineRenderer to draw bullet lines and it works nicely in the editor but not at all in the build (I’m using win7, Unity4.3.0f4, 3d). Some people have solved this by lowering or raising their render settings, others by creating an extra material in the script but not actually even using it (LineRenderer not showing in build - Unity Answers) and for some people it has just suddenly started to work on its own. For me these won’t seem to happen. Am I missing something? Help would be highly appreciated, I’m getting desperate
Here is the code that I’m using at the moment:
`using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour {
public LayerMask collisionMask;
public Transform spawn;
public GameObject gun;
public float timeBetweenShots = 0.3f;
public float damage = 1.0f;
private float nextPossibleShootTime;
private LineRenderer tracer;
private float distance;
private bool revealed;
void Start(){
tracer = GetComponent<LineRenderer>();
revealed = false;
}
void Update(){
if(Input.GetButtonDown("Fire2"))
Shoot();
distance = Vector3.Distance(GameObject.FindGameObjectWithTag("Prey").transform.position, GameObject.FindGameObjectWithTag("Predator").transform.position);
if(distance<=30)
revealed=true;
}
void Shoot(){
if(CanShoot()){
Ray ray = new Ray(spawn.position, -spawn.up);
RaycastHit hit;
Transform hitTransform;
Vector3 hitPoint;
hitTransform = FindClosestHitObject (ray, out hitPoint);
float shotDistance = 25;
if(Physics.Raycast(ray, out hit, shotDistance, collisionMask)){
shotDistance = hit.distance;
if(hitTransform != null)
Debug.Log("We hit: " + hitTransform.name);
if(hit.collider.GetComponent<Entity>()){
hit.collider.GetComponent<Entity>().TakeDamage(damage);
}
}
StartCoroutine("RenderTracer", ray.direction * shotDistance);
nextPossibleShootTime = Time.time + timeBetweenShots;
}
}
Transform FindClosestHitObject (Ray ray, out Vector3 hitPoint){
RaycastHit[] hits = Physics.RaycastAll (ray);
Transform closestHit = null;
float distance = 0;
hitPoint = Vector3.zero;
foreach (RaycastHit hit in hits) {
if(hit.transform != this.transform && (closestHit==null || hit.distance < distance)){
closestHit = hit.transform;
distance = hit.distance;
hitPoint = hit.point;
}
}
return closestHit;
}
private bool CanShoot(){
bool canShoot = true;
if((Time.time < nextPossibleShootTime) || (!revealed)){
canShoot = false;
}
return canShoot;
}
IEnumerator RenderTracer(Vector3 hitPoint){
tracer.enabled=true;
tracer.SetPosition(0, spawn.position);
tracer.SetPosition(1, spawn.position + hitPoint);
yield return null;
tracer.enabled = false;
}
}`
Most of this code is from Sebastian Lague’s youtube Top Down Shooter tutorials, some from quill18creates youtube tutorials and some even my own