I’ve a top down shooter game where the player shoots continuous laser beam , I’ve achieved this with line renderer and raycast. Everything works well the laser shoots and collides with enemy. But i wanted to try some particle effect at the end of collision i’ve provided the hyper link to what i’m implying.
[link text][1]
[1]: https://img.itch.zone/aW1nLzE4NzQzMTAuZ2lm/original/fsRc9m.gif
At the collision i want the particle effect to play. So this is what i’ve did
// Update is called once per frame
void Update()
{
_lineRenderer.SetPosition(0, new Vector3(transform.position.x, transform.position.y, transform.position.z));
RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), transform.up);
if (hit.collider.tag == "Enemy")
{
_lineRenderer.SetPosition(1, new Vector3(hit.point.x, hit.point.y, transform.position.z));
hit.collider.gameObject.GetComponent<Enemy>().Health -= 1;
laserHitEffect = ObjectPooler.SharedInstance.GetPooledObject("LaserBeam");
laserHitEffect.transform.position = hit.point;;
laserHitEffect.SetActive(true);
StartCoroutine(DeactivateLaser());
}
else
{
_lineRenderer.SetPosition(1, transform.up * 2000);
}
}
private IEnumerator DeactivateLaser()
{
yield return new WaitForSeconds(0.1f);
laserHitEffect.SetActive(false);
}
Before using pool i just simply instantiated and destroyed the particle effect object and it worked well the particle effect played at the collision and destroyed in 0.1sec , but instantiating and destroying large number of object again and again is certainly not performance friendly. So i decided to use object pooling.
laserHitEffect = ObjectPooler.SharedInstance.GetPooledObject(“LaserBeam”);
Here in the inspector “LaserBeam” tagged object are pooled and expanded as per necessary in real time.
but my problem is when the laser hit the enemy collider the gameobject gets activated but the Coroutine doesn’t disable the gameobject leaving red trails behind which obviously looks ugly.
can someone help me with this, or else any other ideas to achieve the above effect is also welcome.