Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- // @kurtdekker - part of Jetpack Kurt (Jetpack and SpaceFlight)
- //
- // intention: informed by throttle, this will activate a particle
- // system from a prefab and make a dust spot.
- //
- // DO NOT ADD to a GameObject.
- // Only use the Attach factory function
- public class JetblastFeeler : MonoBehaviour
- {
- Transform nozzle;
- System.Func<float> GetAmount;
- System.Func<float> GetDistance;
- ParticleSystem ps;
- ParticleSystem.EmissionModule em;
- float multiplier;
- public static JetblastFeeler Attach(
- Transform nozzle,
- System.Func<float> GetAmount,
- System.Func<float> GetDistance)
- {
- JetblastFeeler jf = new GameObject ("JetblastFeeler.Attach()").
- AddComponent<JetblastFeeler> ();
- jf.nozzle = nozzle;
- jf.GetAmount = GetAmount;
- jf.GetDistance = GetDistance;
- GameObject go = Instantiate<GameObject> ( Resources.Load<GameObject>(
- "Prefabs/jetblast_basic_prefab"));
- Transform child = go.transform.GetChild (0);
- jf.ps = child.GetComponent<ParticleSystem> ();
- jf.em = jf.ps.emission;
- jf.multiplier = jf.em.rateOverTimeMultiplier;
- return jf;
- }
- // this is so if it was OFF, when it gets flipped on it gets on
- // one frame later, so it doesn't drag the particle system and
- // spew stray particles around the level
- bool enaDelay;
- void Update()
- {
- bool ena = false;
- float amount = GetAmount ();
- if (amount > 0)
- {
- Vector3 perturbance = Quaternion.Euler( 90, 0, 0) * Random.insideUnitCircle * 0.1f;
- Ray ray = new Ray( nozzle.position, nozzle.forward + perturbance);
- RaycastHit rch;
- if (Physics.Raycast (ray, out rch, GetDistance ())
- && !rch.collider.isTrigger)
- {
- ena = true;
- ps.transform.position = rch.point;
- ps.transform.LookAt (rch.point + rch.normal);
- amount *= (1.0f - rch.distance / GetDistance ());
- }
- else
- {
- amount = 0;
- }
- }
- em.enabled = enaDelay && ena;
- em.rateOverTimeMultiplier = amount * multiplier;
- enaDelay = ena;
- }
- void OnDrawGizmos()
- {
- var pos = nozzle.position;
- var dist = GetDistance();
- var pos2 = pos + nozzle.forward * dist;
- Gizmos.DrawLine( pos, pos2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement