[Help] My particles all go away when I stop firing

In the classes, Ben’s particles continue on for the duration of their life. In mine, they all go away when I stop firing. I can’t figure out if the issue is on the particles or in the code. Any chance someone could take a look? In the code, activating and deactivating guns is at the bottom.

image

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

// moves a flying player along x and y axis
public class PlayerController : MonoBehaviour
{

    [Header ("General")]
    [Tooltip("In ms^-1")] [SerializeField] float controlSpeed = 10f; //used in speed calc
    [Tooltip("In m")] [SerializeField] float xRange = 10f;
    [Tooltip("In m")] [SerializeField] float yMinRange = -6f;
    [Tooltip("In m")] [SerializeField] float yMaxRange = 6f;
    [SerializeField] GameObject[] guns;

    [Header ("Screen-position Based")]
    [SerializeField] float positionPitchFactor = -3.8f;
    [SerializeField] float positionYawFactor = 1f;

    [Header ("Control-throw Based")]
    [SerializeField] float controlPitchFactor = -20f;
    [SerializeField] float controlRollFactor = -25f;

    float xThrow, yThrow;
    bool isControlEnabled = true;

    // Use this for initialization


    // Update is called once per frame
    void Update()
    {
        if (isControlEnabled)
        {
            ProcessTranslation();
            ProcessRotation();
            ProcessFiring();
        }
        
    }

    void OnPlayerDeath() //called by string reference
    {
        isControlEnabled = false;
    }

    private void ProcessRotation()
    {
        float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
        float pitchDueToControlThrow = yThrow * controlPitchFactor;

        float pitch = pitchDueToPosition + pitchDueToControlThrow;

        float yaw = transform.localPosition.x + positionYawFactor;

        float roll = xThrow * controlRollFactor;

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

    }

    private void ProcessTranslation()
    {
        xThrow = CrossPlatformInputManager.GetAxis("Horizontal"); //gets horizontal input
        float xOffset = xThrow * controlSpeed * Time.deltaTime; //calculates horizontal distance per frame

        float rawXPos = transform.localPosition.x + xOffset; //sets the new position
        float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

        yThrow = CrossPlatformInputManager.GetAxis("Vertical"); //gets vertical input
        float yOffset = yThrow * controlSpeed * Time.deltaTime;

        float rawYPos = transform.localPosition.y + yOffset; //sets the new position
        float clampedYPos = Mathf.Clamp(rawYPos, yMinRange, yMaxRange);

        transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
    }

    void ProcessFiring()
    {
        if (CrossPlatformInputManager.GetButton("Fire"))
        {
            ActivateGuns();
        }
        else
        {
            DeactivateGuns();
        }
    }

    private void ActivateGuns()
    {
        foreach (GameObject gun in guns)
        {
            gun.SetActive(true);
        }
    }

    private void DeactivateGuns()
    {
        foreach (GameObject gun in guns)
        {
            gun.SetActive(false);
        }
    }
}
1 Like

Well, when your guns aren’t firing your script executes DeactivateGuns, which in-turn literally disables the GameObject. When you disable the object the particle system is a child of (or a component of) you are also disabling the particle system, particles and all.

There is a way to control the emission without having to disable the object. but for now I’ll leave that as a journey of discovery :slight_smile:

1 Like

This topic has solution:

Privacy & Terms