Hey all got a beam working but its got some problems I need to solve… so I added in a yield in the code for the beam to remain active longer on hit of space bar which works fine however if you don’t wait till its done and you spam the space bar it glitches out and plays the sound a ton of times. So how can I fix that? Maybe even add a cool down timer or something and also give it a targeting arc… I am a bit lost so please take a look and see whats wrong and how to improve the script.
[RequireComponent(typeof(LineRenderer))]
public class BeamArray : MonoBehaviour
{
float range = 100.0f;
LineRenderer line;
public Material lineMaterial;
bool phaser = false;
public AudioClip sound;
bool playedsound = false;
void Start()
{
line = GetComponent<LineRenderer>();
line.SetVertexCount(2);
line.renderer.material = lineMaterial;
line.SetWidth(0.1f, 0.25f);
}
void Update()
{
if(Input.GetKeyDown("space") && phaser == false)
{
phaser = true;
}
StartCoroutine (pulsetime());
}
IEnumerator pulsetime()
{
var script = GameObject.Find("aaMain Camera").GetComponent<SelectTarget>();
if(phaser == true)
{
line.enabled = true;
if(phaser == true && playedsound == false)
{
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
playedsound = true;
}
line.SetPosition(0, transform.position);
line.SetPosition(1, script.selectedTarget.transform.position);
yield return new WaitForSeconds(1.680f);
line.enabled = false;
playedsound = false;
phaser = false;
}
}
}