I am a newb in Unity. Can someone explain to me why the particles won’t work when I press the shoot button. They are meant to be an extra thing to spice up the shooting experience. And yes, I did link the script and the particles.
I tried even turning off the mesh-renderer of the gun (which is just a black cube) to see if maybe the cube is just hiding them, but it’s not.
Also, I do have the particles “childrenized” (lol) under the Gun parent.
These are two screenshots of my particles. The second one is if you scroll down under the main section for the particles.
What script? What does your code look like?
@PraetorBlue here is the script, it’s part of the whole gun code script:
using UnityEngine;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public float nextTimeToFire = 0f;
// Update is called once per frame
void Update()
{
if (Input.GetButton(“Fire1”) && Time.time >= nextTimeToFire)
// Or GetButtonDown if not press without letting go
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
void Shoot()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent();
if (target != null)
{
target.TakeDamage(damage);
}
}
}
}
Is the particle system enabled?
Can you show the inspector the gun script?
Ya, I believe that they are enabled, cuz it doesn’t say anywhere that they’re not. But I’m not 100% sure. Btw, here is the gun script in inspector.
@PraetorBlue Btw, everything else works in the script and I don’t receive any error messages.
I’m not really sure. If your shooting itself works, then what you’re showing here should work as far as I can tell. Only other thing I can think of is… what layer is the particles object in? Maybe your camera’s culling mask does not include it?
The culling mask of the camera is set to everything and both the camera and the particles are on the default layer.
And also, how do I know if the particles are enabled?
You’re good there I think. It’s just if the GameObject they are attached to is active, which it is from your screenshots. Where is this particleSystem in relation to the camera? Can you see it playing in scene view when you play? (move scene view out to a separate window to check)
OMG, thank you so much! I’m so dumb♂️! The particle system wasn’t located right. It was under the MAP. Hahaha when I clicked the F shortcut to center, it brought me under the map. Lmao I forgot to place it in front of the gun. All thanks to you, since you asked me where it was in relation to the camera.
1 Like