Advertisement
world_killer

my optimized electricity effect

Jan 14th, 2023
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. #pragma warning disable CS8632
  6. public class ElectricityEffect : ScriptableObject
  7. {
  8.     public float effectSizeWidth;
  9.     public float effectPositionWidth;
  10.     public float positionsBetween;
  11.     public float positionOffset;
  12.  
  13.     public Vector3 pos1, pos2;
  14.     Vector3 direction;
  15.  
  16.     GameObject effect;
  17.     List<Vector3> positions = new List<Vector3>();
  18.     List<GameObject> effects = new List<GameObject>();
  19.  
  20.     public ElectricityEffect(){}
  21.  
  22.     public void Initiate(GameObject effect, Vector3 pos1, Vector3 pos2, float positionsBetween, float positionOffset)
  23.     {
  24.         this.effect = effect;
  25.         this.pos1 = pos1;
  26.         this.pos2 = pos2;
  27.         this.positionsBetween = positionsBetween;
  28.         this.positionOffset = positionOffset;
  29.  
  30.         direction = pos2 - pos1;
  31.  
  32.         CreateEffects();
  33.     }
  34.  
  35.     void CreateEffects()
  36.     {
  37.         Vector3 length = direction / positionsBetween;
  38.         GameObject? latestEffect = null;
  39.  
  40.         for (int i = 0; i < positionsBetween+2; i++)
  41.         {
  42.             positions.Add(pos1 + length * i);
  43.             latestEffect = Instantiate(effect);
  44.             effects.Add(latestEffect);
  45.         }
  46.  
  47.         effects.RemoveAt(effects.Count - 1);
  48.         Destroy(latestEffect);
  49.     }
  50.  
  51.     public void UpdateEffect()
  52.     {
  53.         List<Vector3> randomOffsets = new List<Vector3>();
  54.  
  55.         randomOffsets.Add(pos1);
  56.         for (int i = 1; i < positions.Count - 1; i++)
  57.         {
  58.             randomOffsets.Add(positions[i] + new Vector3(Random.Range(positionOffset, positionOffset), Random.Range(-positionOffset, positionOffset), Random.Range(-positionOffset, positionOffset)));
  59.         }
  60.         randomOffsets.Add(pos2);
  61.  
  62.         for (int i = 0; i < randomOffsets.Count - 1; i++)
  63.         {
  64.             Vector3 halfWay = (randomOffsets[i + 1] - randomOffsets[i]) / 2;
  65.             effects[i].transform.position = randomOffsets[i] + halfWay;
  66.             effects[i].transform.LookAt(randomOffsets[i + 1]);
  67.             effects[i].transform.localScale = new Vector3(0.05f, 0.05f, (randomOffsets[i] - randomOffsets[i + 1]).magnitude);
  68.         }
  69.     }
  70.  
  71.     public void Disable()
  72.     {
  73.         foreach(GameObject effect in effects)
  74.         {
  75.             effect.SetActive(false);
  76.         }
  77.     }
  78.  
  79.     public void Enable()
  80.     {
  81.         foreach (GameObject effect in effects)
  82.         {
  83.             effect.SetActive(true);
  84.         }
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement