Advertisement
PatchQuest

Untitled

Sep 13th, 2019
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. // Allows particle effects to follow a GameObject without being parented to that GameObject.
  5. // This way, destroying the track target doesn't also destroy the effect.
  6.  
  7. public class EffectTracker : MonoBehaviour {
  8.  
  9.     // A list of effect roots and their corresponding targets
  10.     private static List<(GameObject effect, GameObject target)> trackers = new List<(GameObject, GameObject)> ();
  11.  
  12.     // Add an effect to the list
  13.     public static void Track (GameObject effect, GameObject target) {
  14.         trackers.Add ((effect, target));
  15.     }
  16.  
  17.     // Update all tracked effects
  18.     void Update () {
  19.  
  20.         // Loop backwards through every tracked effect
  21.         for (int i = trackers.Count - 1; i >= 0; i--) {
  22.             var (effect, target) = trackers[i];
  23.  
  24.             // If either the effect or the target has been destroyed...
  25.             if (effect == null || target == null) {
  26.                 // Stop tracking this effect
  27.                 trackers.RemoveAt (i);
  28.             }
  29.  
  30.             // Otherwise...
  31.             else {
  32.                 // Change the world position of the effect
  33.                 effect.transform.position = target.transform.position;
  34.             }
  35.         }
  36.     }
  37.    
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement