Player won't explode after collision

Good evening,
After my rocketship collides with an enemy, the player disappears but he doesn’t explode. I recently used the code frome this link here:
Stop Lasers Firing After Player Collision - Unity Courses / Talk - GameDev.tv
to stop my lasers emission after colliding. After using the above code, my laser emissions have stopped but now my explosion won’t go off.

Here is my player controls program:
using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class playerControls : MonoBehaviour

{

[Header("General Settings")]

[Tooltip("How fast ship moves up and down based on player input.")]

[SerializeField] float xSpeed = 5f;

[Tooltip("How fast ship moves up and down based on player input.")]

[SerializeField] float ySpeed = 5f;

// [SerializeField] float yRange = 3f;

[Header("Airplane Simulated Movement.")]

[SerializeField] float positionPitchFactor = -2f;

[SerializeField] float controlPitchFactor = -10f;

[SerializeField] float positionYawFactor = 2f;

[SerializeField] float controlRollFactor = -20f;

[Header("Restriction of ship movement on the y-axis.")]

[Tooltip("Used to keep the ship to stay onscreen")] [SerializeField] float yRangeMin = 4f;

[Tooltip("Used to keep the ship to stay onscreen")] [SerializeField] float yRangeMax = 6f;



[Header("Restirction of ship movement on the x-axis.")]

[Tooltip("Used to keep the ship to stay onscreen")] [SerializeField] float xRange = 5f;

[Tooltip("Used to keep the ship to stay onscreen")][Header("Particle Systems")]

[SerializeField] GameObject[] laser;

[SerializeField] GameObject[] shotgunLaser;

float xThrow, yThrow;

// Update is called once per frame

void Update()

{

    ProcessTranslation();

    ProcessRotation();

    ProcessFiring();

}

void ProcessRotation()

{

    float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;

    float pitchDueToControl = yThrow * controlPitchFactor;

   

    float pitch = (pitchDueToControl + pitchDueToControl);

    float yaw = (transform.localPosition.x * positionYawFactor);

    float roll = (xThrow * controlRollFactor);

   

    transform.localRotation = Quaternion.Euler(pitch, yaw, roll);

}

void ProcessTranslation()

{

    xThrow = Input.GetAxis("Horizontal") * Time.deltaTime;

    yThrow = Input.GetAxis("Vertical") * Time.deltaTime;

    float xOffset = xThrow * xSpeed;

    float xRawPos = xOffset + transform.localPosition.x;

    float yOffset = yThrow * ySpeed;

    float yRawPos = yOffset + transform.localPosition.y;

    float clampedXPos = Mathf.Clamp(xRawPos, -xRange, xRange);

    float clampedYPos = Mathf.Clamp(yRawPos, -yRangeMin, yRangeMax);

    // Debug.Log("Turning left or right: " + horinzontalThrow);

    // Debug.Log("Turning up or down: " + verticalThrow);

    transform.localPosition = new Vector3(clampedXPos, clampedYPos,

    transform.localPosition.z);

}

public void ProcessFiring()

{

    if (Input.GetButton("Fire1"))

    {

        // Debug.Log("Firing Laser Bullets.");

        SetMainLasersActive(true);

    }

    else if (Input.GetButton("Fire2"))

    {

        // Debug.Log("Firing Shotgun Shells.");

        SetShotgunLasersActive(true);

    }

    else

    {

        // Debug.Log("Not firing.");

        SetMainLasersActive(false);

        SetShotgunLasersActive(false);

    }

}

public void DeactivateLasers()

{

    foreach(GameObject mainLasers in laser)

    {

        mainLasers.SetActive(false);

    }

    foreach(GameObject shotgunLasers in shotgunLaser)

    {

        shotgunLasers.SetActive(false);

    }

}

void ActivateShotgunLasers()

{

    foreach(GameObject shotgunLasers in shotgunLaser)

    {

        shotgunLasers.SetActive(true);

    }

}

void SetMainLasersActive(bool isActive)

{

    foreach(GameObject Mainlasers in laser)

    {

        var emissionModule = Mainlasers.GetComponent<ParticleSystem>().emission;

        emissionModule.enabled = isActive;

    }

}

void SetShotgunLasersActive(bool isActive)

{

    foreach(GameObject ShotgunLasers in shotgunLaser)

    {

        var emissionModule = ShotgunLasers.GetComponent<ParticleSystem>().emission;

        emissionModule.enabled = isActive;

    }

}

}

and here is my playerCollisions
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

public class playerCollision : MonoBehaviour

{

[SerializeField] ParticleSystem ExplosionSFX;

[SerializeField] GameObject playerShip;

[SerializeField] float delay;

void OnTriggerEnter(Collider other)

{

    // Debug.Log(this.name + "**collided with**" + other.gameObject.name);  

    StartCrashSequence();

}

void StartCrashSequence()

{

    var setControls = GetComponent<playerControls>().enabled;

    Invoke("ReloadLevel", delay);

    ExplosionSFX.Play();

    Debug.Log("Explosion Playing");

   

    var lasers = FindObjectsOfType<ParticleSystem>();

    foreach (ParticleSystem laser in lasers)

    {

        laser.Stop();

    }

    Erase();        

}

void Erase()

{

    GetComponent<BoxCollider>().enabled = false;

    GetComponent<MeshRenderer>().enabled = false;

    GetComponent<playerControls>().enabled = false;

}

void ReloadLevel()

{

    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

    SceneManager.LoadScene(currentSceneIndex);

}

}

Please reply at your earliest conveinence.

1 Like

Hi!

The issue lies in the following lines of code:

    ExplosionSFX.Play(); // Plays the explosion particles

    Debug.Log("Explosion Playing");

    // You are creating a list of all the particles in the scene.
    // That includes your explosion and unrelated to the player particles.
    var lasers = FindObjectsOfType<ParticleSystem>();

    foreach (ParticleSystem laser in lasers)
    {
        laser.Stop(); // Stops the explosion particles
    }

As you can see there, your explosion particles get to play, but then they are told to stop in the foreach loop.

The easiest solution is to play the explosion after you stop all the particle effects.

    foreach (ParticleSystem laser in lasers)
    {
        laser.Stop();
    }

    ExplosionSFX.Play(); //Explosion should be here instead.

    Erase();        

}

Keep in mind that this doesn’t fix other unwanted behaviors, like making enemies’ explosions and lasers stop, same goes for other types of particles effects like environment art and so on, you might want to check how to stop only the lasers instead of every particle system in the scene.

Hope this helps!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms