Lasers not firing at all

Hi, im on the lesson about arrays and foreach loops and when i followed along with the instructor the code doesn’t run properly. When I press the fire button it says emission is enabled (And Debug.Log statements are working) but I don’t see any particles flying out of the ship, i’ve checked to see if i’ve maybe deleted them but i checked and they are still there. Here is my code just in case I messed up something:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControls : MonoBehaviour
{
[SerializeField] float controlSpeed;
[SerializeField] float xRange;
[SerializeField] float yRange;
[SerializeField] GameObject lasers;

[SerializeField] float positionPitchFactor;
[SerializeField] float controlPitchFactor;

[SerializeField] float positionYawFactor;

[SerializeField] float controlRollFactor;



float xThrow, yThrow;

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

}

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

	// rotation on the 3 different axes
	float pitch =  pitchDueToPosition + pitchDueToControlThrow;
    float yaw = transform.localPosition.x * positionYawFactor;
    float roll = xThrow * controlRollFactor;

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

void ProcessTranslation()
{
    xThrow = Input.GetAxis("Horizontal");
    yThrow = Input.GetAxis("Vertical");

    float xOffset = xThrow * Time.deltaTime * controlSpeed;
    float yOffset = yThrow * Time.deltaTime * controlSpeed;

    float rawXPos = transform.localPosition.x + xOffset;
    float rawYPos = transform.localPosition.y + yOffset;

    float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);
    float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

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

void ProcessFiring()
{ 
    if (Input.GetButton("Fire1"))
    {
        ActivateLasers();
    }
    else
    {
        DeativateLasers();

	}
}

void ActivateLasers()
{
    foreach(GameObject laser in lasers)
    {
        var emissionModule = laser.GetComponent<ParticleSystem>().emission;
        emissionModule.enabled = true;
        Debug.Log("pressed");
    }
}

void DeativateLasers()
{
    // for each of the lasers we have, activate them
    foreach (GameObject laser in lasers)
    {
        var emissionModule = laser.GetComponent<ParticleSystem>().emission;
        emissionModule.enabled = false;
        Debug.Log("not");
    }
    }

}

Hi,

Did you assign the child game object of the player to the lasers field in the PlayerControls component in the Inspector? If so, are “Play On Awake” and “Loop” enabled in the Particle Systems?


See also:

1 Like

Thank you so much! It was just that play on awake was turned off so turning it on helped.

I appreciate the help :smiley:

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

Privacy & Terms