I made a simple spaceship that has a particlesystem. When I press “space” button, spaceship should fly and particle system should instantiate and play. But it’s not playing. It seems in hierarchy as clone but not playing.
Those are codes :
void FlyShip()
{
if (Input.GetKey(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce);
if (!takeoffSound.isPlaying)
{
// _rocketJetParticle is gameobject.
_rocketJetParticle = Instantiate(rocketJetParticle, new Vector3(transform.position.x, transform.position.y - 4, transform.position.z), transform.rotation);
takeoffSound.Play();
}
}
else
{
//Destroy(_rocketJetParticle);
takeoffSound.Stop();
}
}
Your not really doing it the best way and my way is not the best either but easier.
- Create Particle System
- Make sure to uncheck ‘Play On Awake’
- Add Particle system to rocket as child
- Position Particle system on the rocket at the boosters
- Add [SerializeField] private ParticleSystem _rocketJetParticle; to variable list.
- Change void FlyShip() as per below.
- Add Particle systems to script in inspector.
FlyShip()
{
if (Input.GetKey(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce);
if (!takeoffSound.isPlaying)
{
// Start Particle System.
_rocketJetParticle.Play();
takeoffSound.Play();
}
}
else
{
_rocketJetParticle.Stop();
takeoffSound.Stop();
}
}
You shouldn’t Instantiate (and destroy) the particle system all the time, just let it exist all the time as a child object and do play and stop.
I don’t know when you run FlyShip(), but if it’s in update this will result in creating a new particle system every frame, which is not what you want.
public ParticleSystem oThruster; //set in inspector by dragging the particle system to the variable
ParticleSystem.EmissionModule oThrusterEmission;
oThrusterEmission = oThruster.emission;
//then set on
oThrusterEmission.enabled = true;
//or off
oThrusterEmission.enabled = false;