Line renderer not working
So I was trying to make a very simple bullet which literally just casts a line from the tip of the gun forward the same amount as a raycast. I'm thinking my issue may have something to do with the fact that the accessed line renderer isn't on the same object as the script but I did try this before and it seemed to work. I would put the line renderer on the same object but it's already in use by another script on the object. I'll put the code I think is relevant here:
RaycastHit hit; Ray isGrounded = new ray (transform.position, cam.forward); if (Physics.RayCast (isGrounded, out hit, reach)){ line.SetPosition(1, pistol.transform.position); line.SetPosition(2, hit.point); }}
I understand the line should only be drawn when there is a raycast hit, but I have also set up the print function so I know when it should be called. I'm really lost with this ass it worked fine with practically identical code on the other script so I figure it must be something to do with the line renderer not actually being attached to the player's object. Please help.
Answer by TheAnimatedKiddo · Sep 30, 2020 at 10:06 AM
Hey @Deathleaper! Try this code and when the shoot function, call the line renderer. I modified this code from Brackyes, just so you know.
using UnityEngine; using System.Collections; using UnityEngine.UI; using JetBrains.Annotations; public class OSGun : MonoBehaviour { public float damage = 10f; public float range = 100f; public int maxAmmo = 10; private int currentAmmo; public float reloadTime = 1f; private bool isReloading = false; public Camera fpsCam; public ParticleSystem muzzleFlash; public Transform shotText; public Animator animator; public AudioSource audioData; void Start() { currentAmmo = maxAmmo; audioData = GetComponent<AudioSource>(); } void OnEnable() { isReloading = false; animator.SetBool("Reloading", false); animator.SetBool("Shooting", false); shotText.GetComponent<Text>().text = currentAmmo + "/" + maxAmmo.ToString(); } void Update() { if (isReloading) { return; } if (currentAmmo <= 0) { StartCoroutine(Reload()); return; } if (Input.GetButtonDown("Fire1")) { animator.SetBool("Shooting", true); Shoot(); shotText.GetComponent<Text>().text = currentAmmo + "/" + maxAmmo.ToString(); } if (Input.GetButtonUp("Fire1")) { animator.SetBool("Shooting", false); } if (Input.GetKey("r")) { if (currentAmmo == maxAmmo) { return; } else { StartCoroutine(Reload()); return; } } } IEnumerator Reload () { isReloading = true; Debug.Log("Reload..."); animator.SetBool("Reloading", true); yield return new WaitForSeconds(reloadTime - .25f); animator.SetBool("Reloading", false); yield return new WaitForSeconds(.25f); currentAmmo = maxAmmo; shotText.GetComponent<Text>().text = maxAmmo + "/" + maxAmmo.ToString(); isReloading = false; } void Shoot() { muzzleFlash.Play(); currentAmmo--; audioData.Play(0); RaycastHit hit; if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) { Debug.Log(hit.transform.name); Target target = hit.transform.GetComponent<Target>(); if (target != null) { target.TakeDamage(damage); } } else if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) { Debug.Log(hit.transform.name); GlassTarget target = hit.transform.GetComponent<GlassTarget>(); if (target != null) { target.TakeDamage(damage); } } } public void SetAmmoOnEquip() { shotText.GetComponent<Text>().text = currentAmmo + "/" + maxAmmo.ToString(); } public void SetAmmoOnUnequip() { shotText.GetComponent<Text>().text = "--"; } }