I am currently working on a script to control a laser beam, that on hit with certain objects emits a particle effect at the point of collision.
However the issue is I am unable to figure out how to only Instantiate a single emitter at a time so as not to have too many draw calls.
I have tried several different methods, including WaitForSeconds(), InvokeRepeating() and using bools but to no avail.
Here is the code as it stands, it Instatiates a single particle emitter every frame at the moment. I am probably being an idiot and the solution is staring me in the face but anyway you’ll find the code below.
public class LaserWall : MonoBehaviour
{
RaycastHit hitCheck;
public float laserLength = 20.0f;
public GameObject hitEffect;
public GameObject spaceShip;
void Start ()
{
GetComponent<LineRenderer> ().SetPosition (1, Vector3.forward * laserLength);
}
void FixedUpdate ()
{
LaserCollisionCheck();
}
void LaserCollisionCheck ()
{
if (Physics.Raycast (transform.position, -Vector3.up, out hitCheck, laserLength) && GetComponent<LineRenderer> ().enabled == true) {
Debug.Log ("Hit");
if (hitCheck.collider.name == "space_ship") {
Instantiate (hitEffect, hitCheck.point, Quaternion.identity);
GetComponent<LineRenderer> ().SetPosition (1, Vector3.forward * hitCheck.distance);
//spaceShip.GetComponent<SpaceshipController>().Explode();
}
if (hitCheck.collider.name == "laser_wall") {
Instantiate (hitEffect, hitCheck.point, Quaternion.identity);
GetComponent<LineRenderer> ().SetPosition (1, Vector3.forward * laserLength);
}
}
}
}
Thanks in advance to anyone that can help, just to clarify I am trying to reduce the number of particle emitters being created to around 1 per second.
~ byteCrunch