Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- #pragma warning disable CS8632
- public class ElectricityEffect : ScriptableObject
- {
- public float effectSizeWidth;
- public float effectPositionWidth;
- public float positionsBetween;
- public float positionOffset;
- public Vector3 pos1, pos2;
- Vector3 direction;
- GameObject effect;
- List<Vector3> positions = new List<Vector3>();
- List<GameObject> effects = new List<GameObject>();
- public ElectricityEffect(){}
- public void Initiate(GameObject effect, Vector3 pos1, Vector3 pos2, float positionsBetween, float positionOffset)
- {
- this.effect = effect;
- this.pos1 = pos1;
- this.pos2 = pos2;
- this.positionsBetween = positionsBetween;
- this.positionOffset = positionOffset;
- direction = pos2 - pos1;
- CreateEffects();
- }
- void CreateEffects()
- {
- Vector3 length = direction / positionsBetween;
- GameObject? latestEffect = null;
- for (int i = 0; i < positionsBetween+2; i++)
- {
- positions.Add(pos1 + length * i);
- latestEffect = Instantiate(effect);
- effects.Add(latestEffect);
- }
- effects.RemoveAt(effects.Count - 1);
- Destroy(latestEffect);
- }
- public void UpdateEffect()
- {
- List<Vector3> randomOffsets = new List<Vector3>();
- randomOffsets.Add(pos1);
- for (int i = 1; i < positions.Count - 1; i++)
- {
- randomOffsets.Add(positions[i] + new Vector3(Random.Range(positionOffset, positionOffset), Random.Range(-positionOffset, positionOffset), Random.Range(-positionOffset, positionOffset)));
- }
- randomOffsets.Add(pos2);
- for (int i = 0; i < randomOffsets.Count - 1; i++)
- {
- Vector3 halfWay = (randomOffsets[i + 1] - randomOffsets[i]) / 2;
- effects[i].transform.position = randomOffsets[i] + halfWay;
- effects[i].transform.LookAt(randomOffsets[i + 1]);
- effects[i].transform.localScale = new Vector3(0.05f, 0.05f, (randomOffsets[i] - randomOffsets[i + 1]).magnitude);
- }
- }
- public void Disable()
- {
- foreach(GameObject effect in effects)
- {
- effect.SetActive(false);
- }
- }
- public void Enable()
- {
- foreach (GameObject effect in effects)
- {
- effect.SetActive(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement