Advertisement
Guest User

Unity Jetblast feeler class

a guest
Aug 3rd, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // @kurtdekker - part of Jetpack Kurt (Jetpack and SpaceFlight)
  6. //
  7. // intention: informed by throttle, this will activate a particle
  8. // system from a prefab and make a dust spot.
  9. //
  10. // DO NOT ADD to a GameObject.
  11. // Only use the Attach factory function
  12.  
  13. public class JetblastFeeler : MonoBehaviour
  14. {
  15.     Transform nozzle;
  16.     System.Func<float> GetAmount;
  17.     System.Func<float> GetDistance;
  18.  
  19.     ParticleSystem ps;
  20.     ParticleSystem.EmissionModule em;
  21.  
  22.     float multiplier;
  23.  
  24.     public static JetblastFeeler Attach(
  25.         Transform nozzle,
  26.         System.Func<float> GetAmount,
  27.         System.Func<float> GetDistance)
  28.     {
  29.         JetblastFeeler jf = new GameObject ("JetblastFeeler.Attach()").
  30.             AddComponent<JetblastFeeler> ();
  31.  
  32.         jf.nozzle = nozzle;
  33.         jf.GetAmount = GetAmount;
  34.         jf.GetDistance = GetDistance;
  35.  
  36.         GameObject go = Instantiate<GameObject> ( Resources.Load<GameObject>(
  37.             "Prefabs/jetblast_basic_prefab"));
  38.  
  39.         Transform child = go.transform.GetChild (0);
  40.         jf.ps = child.GetComponent<ParticleSystem> ();
  41.         jf.em = jf.ps.emission;
  42.         jf.multiplier = jf.em.rateOverTimeMultiplier;
  43.  
  44.         return jf;
  45.     }
  46.  
  47.     // this is so if it was OFF, when it gets flipped on it gets on
  48.     // one frame later, so it doesn't drag the particle system and
  49.     // spew stray particles around the level
  50.     bool enaDelay;
  51.  
  52.     void Update()
  53.     {
  54.         bool ena = false;
  55.         float amount = GetAmount ();
  56.  
  57.         if (amount > 0)
  58.         {
  59.             Vector3 perturbance = Quaternion.Euler( 90, 0, 0) * Random.insideUnitCircle * 0.1f;
  60.                
  61.             Ray ray = new Ray( nozzle.position, nozzle.forward + perturbance);
  62.             RaycastHit rch;
  63.             if (Physics.Raycast (ray, out rch, GetDistance ())
  64.                 && !rch.collider.isTrigger)
  65.             {
  66.                 ena = true;
  67.  
  68.                 ps.transform.position = rch.point;
  69.                 ps.transform.LookAt (rch.point + rch.normal);
  70.  
  71.                 amount *= (1.0f - rch.distance / GetDistance ());
  72.             }
  73.             else
  74.             {
  75.                 amount = 0;
  76.             }
  77.         }
  78.  
  79.         em.enabled = enaDelay && ena;
  80.         em.rateOverTimeMultiplier = amount * multiplier;
  81.  
  82.         enaDelay = ena;
  83.     }
  84.  
  85.     void OnDrawGizmos()
  86.     {
  87.         var pos = nozzle.position;
  88.  
  89.         var dist = GetDistance();
  90.  
  91.         var pos2 = pos + nozzle.forward * dist;
  92.  
  93.         Gizmos.DrawLine( pos, pos2);
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement