WalkParticles.cs 869 Bytes
Newer Older
Luke Smith's avatar
Luke Smith committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
using UnityEngine;
using System.Collections;

public class WalkParticles : MonoBehaviour {

    private ParticleSystem ps;
    private ParticleSystem.EmissionModule module;
    private HeroMobility hm;
    public float speedThreshold = 0.001f;


    // Use this for initialization
    void Start () {
        ps = GetComponentInChildren<ParticleSystem>();
        module = ps.emission;
        hm = GetComponent<HeroMobility>();
    }

    // Update is called once per frame
    void LateUpdate () {
        if (hm.deltaPos.sqrMagnitude > speedThreshold) {
            if (!ps.isPlaying) {
                ps.Simulate(0.0f, true, false);
                module.enabled = true;
                ps.Play();
            }
        } else {
            if (ps.isPlaying) {
                module.enabled = false;
                ps.Stop();
            }
        }
    }
}