- Home /
[Tank Game Tutorial] Particles Only Appear While Turning
So I am following the tank game tutorial and have run into an issue with the particle system, happens on the included completed version too so it's not something I did. The dust particles for the tank tracks only work when turning. The particles do not appear when going forward or backward. What could be the cause of this? I have no idea since I am so new.
I recreated the particle system and it caused the same issues. I am not sure what needs to be done or what could be the cause.
gif showing issue: https://i.gyazo.com/ed25fbdfd1d50da5300e1e01c07c6bf8.gif
Thanks for any help.
Added per the suggestion of @ThePersister. The particle system settings and the tank hierarchy.
Since the particles are fine. It's probably the code that is used to turn the particles on and off that is flawed. Could you copy your current code where it (likely) says particleSystem.Play() and particleSystem.Stop() and post it here in a comment or edit your main post?
Once you've done that I can help you out :)
There is no code running that particle system. The only thing that I can gather it's using is the particle systems own "Emission: Rate over Distance".
I should have mentioned this issue only appeared after I updated to Unity 5.5. I was using 5.4 before. I know the particle system had changes but I'm not aware enough of Unity to know what exactly and if it might have effected the tutorial.
Oooh interesting. I haven't gone beyond Unity 5.4 yet. Documentation here: https://docs.unity3d.com/$$anonymous$$anual/PartSysEmission$$anonymous$$odule.html
It says that if that rate per distance mode is active it will act based upon the movement of the Parent object. Now this may be a far guess, but perhaps there's a parent between the particle system itself and the core of the vehicle, like a left side parent or right side parent. And supposedly the particle system reacts to rotations as well, and those objects are only ever moved locally when they're rotated by the parent then that could explain the dust particles for rotational movement only.
Once again, that's a really far guess though.
$$anonymous$$aybe @Bunny83 knows more? :) (he should get a notification by this reference of $$anonymous$$e)
In the meantime, could you show the structure of the tank in the hierarchy? And perhaps the exact settings in your particle system, that might help the next person with figuring this out for you!
Answer by kevkevatthesev · Dec 27, 2016 at 11:16 PM
I'm using Unity 5.5.0, and I wasn't able to get the script @ThePersister wrote to work properly on its own for some reason, but I did successfully merge the code into TankMovement.cs, and it appears to be working. My solution doesn't exactly seem ideal, since the particle system is relying on the keyboard input, not the actual speed of the tank (But it looks ok).
I changed Update() to the following:
private void Update()
{
// Store the player's input and make sure the audio for the engine is playing.
m_MovementInputValue = Input.GetAxis (m_MovementAxisName);
m_TurnInputValue = Input.GetAxis (m_TurnAxisName);
EngineAudio ();
if (Mathf.Abs (m_MovementInputValue) < 0.1f && Mathf.Abs (m_TurnInputValue) < 0.1f) {
_updateParticleSystem( m_leftDustTrail, false );
_updateParticleSystem( m_rightDustTrail, false );
} else {
_updateParticleSystem( m_leftDustTrail, true );
_updateParticleSystem( m_rightDustTrail, true );
}
}
And I copied the _updateParticleSystem function without any changes:
private void _updateParticleSystem( ParticleSystem particleSystem, bool shouldBePlaying )
{
if ( particleSystem.isPlaying && !shouldBePlaying )
{
// Stop if currently playing and it shouldn't be.
particleSystem.Stop();
}
else if ( !particleSystem.isPlaying && shouldBePlaying )
{
// Play if currently not playing and it should be.
particleSystem.Play();
}
}
}
Add new variables to the top of the class:
public ParticleSystem m_leftDustTrail;
public ParticleSystem m_rightDustTrail;
Do make sure that the settings for both particle systems are changed as mentioned:
"Right now you have Rate over Distance set to 10, set that to 0. And set Rate over Time to 10. Also, set Play on Awake to false. Do this for both dustTrail Particle Systems."
Hope this helps.
Answer by ThePersister · Dec 06, 2016 at 10:02 AM
Hi @jddg5wa,
Thank you for providing the extra information. Since I can't upgrade to 5.5 right now to provide specific help, I'll give you an alternative solution.
This will work if the particleSystems are not stopped and played by other scripts. Attach the following script to the tank object:
using UnityEngine;
using System.Collections;
public class TankParticlesExample : MonoBehaviour {
public ParticleSystem m_leftDustTrail;
public ParticleSystem m_rightDustTrail;
private Rigidbody m_rigidBody;
void Start ()
{
m_rigidBody = GetComponent<Rigidbody>();
if (m_leftDustTrail == null)
{
Debug.LogError( "Don't forget to assign the LeftDustTrail in the inspector." );
}
if( m_rightDustTrail == null )
{
Debug.LogError( "Don't forget to assign the RightDustTrail in the inspector." );
}
}
void Update()
{
// If velocity is greater than 0.5f, show particles, else, don't.
if( m_rigidBody.velocity.magnitude > 0.5f)
{
_updateParticleSystem( m_leftDustTrail, true );
_updateParticleSystem( m_rightDustTrail, true );
}
else
{
_updateParticleSystem( m_leftDustTrail, false );
_updateParticleSystem( m_rightDustTrail, false );
}
}
private void _updateParticleSystem( ParticleSystem particleSystem, bool shouldBePlaying )
{
if ( particleSystem.isPlaying && !shouldBePlaying )
{
// Stop if currently playing and it shouldn't be.
particleSystem.Stop();
}
else if ( !particleSystem.isPlaying && shouldBePlaying )
{
// Play if currently not playing and it should be.
particleSystem.Play();
}
}
}
Make sure to assign the ParticleSystems in the inspector, just drag and drop. Right now you have Rate over Distance set to 10, set that to 0. And set Rate over Time to 10. Also, set Play on Awake to false. Do this for both dustTrail Particle Systems.
That should make it all work nicely. You can adjust the value of 0.5f to your needs, a higher value would require a greater speed before showing dust trails.
If it doesn't work, the Particle Systems are probably set to play and stop by other scripts. In that case you can remove the particle systems (temporarily) to find out where that happens, because if you remove them and run the game, you should get an error. Double clicking on that error will lead you to its origin.
With that, you should be able to get it to work. I hope this fulfills your request.
If this helped, please accept my answer, it'd be much appreciated!
If you need more details, let me know!
Best of luck!
Cheers,
ThePersister