Created
June 17, 2015 01:39
-
-
Save ashblue/03100f623c5eac72e724 to your computer and use it in GitHub Desktop.
Unity particle thrower.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class ParticleThrower : MonoBehaviour { | |
public class ParticleData { | |
public Rigidbody2D body; | |
public Vector2 force; | |
public float torque; | |
public ParticleData (Rigidbody2D body, Vector2 minForce, Vector2 maxForce, float torqueMin, float torqueMax) { | |
this.body = body; | |
float forceX = Random.Range(minForce.x, maxForce.x); | |
float forceY = Random.Range(minForce.y, maxForce.y); | |
force = new Vector2(forceX, forceY); | |
torque = Random.Range(torqueMin, torqueMax); | |
} | |
} | |
// Storage of pre-calculate particle throwing | |
List<ParticleData> particles = new List<ParticleData>(); | |
[Tooltip("Immediately throw when enabled")] | |
[SerializeField] bool throwOnEnable; | |
[Tooltip("Use this heading if throwOnEnabled is checked")] | |
[SerializeField] float headingX = 1f; | |
[Tooltip("How long to wait before removing items from the scene. Leave at 0 to never cleanup")] | |
[SerializeField] float cleanupDealy = 20f; | |
[Tooltip("Over how long a period of time will items fade out")] | |
[SerializeField] float fadeTime = 3f; | |
[Tooltip("Random throw ranges")] | |
[SerializeField] Vector2 minForce = new Vector2(200f, 0f); | |
[SerializeField] Vector2 maxForce = new Vector2(350f, 100f); | |
[SerializeField] float minTorque = 50f; | |
[SerializeField] float maxTorque = 90f; | |
void Awake () { | |
// Pre generate throw data to prevent fps hiccups | |
foreach (Transform child in transform) { | |
particles.Add( | |
new ParticleData(child.GetComponent<Rigidbody2D>(), | |
minForce, maxForce, | |
minTorque, maxTorque) | |
); | |
} | |
} | |
void Start () { | |
if (throwOnEnable) Throw(headingX); | |
} | |
public void Throw () { | |
Throw(headingX); | |
} | |
public void Throw (float headingX) { | |
headingX = Mathf.Clamp(headingX, -1f, 1f); | |
// Force detach particles from object since it will be destroyed | |
transform.parent = transform.parent.parent; | |
foreach (ParticleData part in particles) { | |
part.force.x *= headingX; | |
part.torque *= headingX; | |
part.body.AddForce(part.force); | |
part.body.AddTorque(part.torque); | |
} | |
particles = null; | |
if (cleanupDealy != 0f) { | |
StartCoroutine(Cleanup(cleanupDealy, fadeTime)); | |
} | |
} | |
IEnumerator Cleanup (float delay, float fadeTime) { | |
yield return new WaitForSeconds(delay); | |
SpriteRenderer[] sprites = GetComponentsInChildren<SpriteRenderer>(); | |
float timer = 0.0f; | |
while (timer <= 1.0f) { | |
foreach (SpriteRenderer s in sprites) { | |
Color c = s.color; | |
c.a = 1f - timer; | |
s.color = c; | |
} | |
timer += Time.deltaTime / fadeTime; | |
yield return null; | |
} | |
// Clear self | |
Destroy(gameObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment