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");
}
}
}