Advertisement
MaximilianPs

Unity - FireLight

Sep 11th, 2021
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. /// <ToDo>
  5. ///  IMPLEMENT PARAMETERS FOR DIFFERENT LIGHT
  6. /// </ToDo>
  7. enum EmitterType
  8. {
  9.     candle,
  10.     fire,
  11.     bulb,
  12.     neon
  13. }
  14.  
  15. [RequireComponent(typeof(Light))]
  16. public class FireLight : MonoBehaviour
  17. {
  18.     #region Variables
  19.  
  20.     [Tooltip("Maximum possible intensity the light can flicker to")]
  21.     [SerializeField, Min(0)] float maxIntensity = 1.5f;
  22.     [Tooltip("Minimum possible intensity the light can flicker to")]
  23.     [SerializeField, Min(0)] float minIntensity = 0.5f;
  24.     [Space]
  25.     [Tooltip("Maximum light range")]
  26.     [SerializeField] float maxRange = 5.7f;
  27.     [Tooltip("Minimum light range")]
  28.     [SerializeField] float minRange = 0.5f;
  29.     [Space]
  30.     [Tooltip("How much the light will move around to have a more visible fire effect")]
  31.     [SerializeField] float ligthDisplacement = 0.25f;
  32.     [Space]
  33.     [SerializeField] Color colorA = new Color(227, 173, 78);
  34.     [SerializeField] Color colorB = new Color(227, 139, 78);
  35.     [Space]
  36.     [Tooltip("how fast the light will flicker: Low (3 or 5) Fire or Candle - Hight: dying Neon")]
  37.     [SerializeField, Range(0, 50)] float smoothness = 3f;
  38.     [Space]
  39.     [Tooltip("If true, will set the light to cast shadows by changing the LightSource parameters")]
  40.     [SerializeField] bool castShadows = false;
  41.  
  42.     float maxFlickerFrequency = 1f;
  43.     float minFlickerFrequency = 0.1f;
  44.  
  45.     float nextIntensity;
  46.  
  47.     float nextRange;
  48.  
  49.     float flickerFrequency;
  50.     float timeOfLastFlicker;
  51.  
  52.     Vector3 lPos,curPos, newPos;
  53.  
  54.     #endregion
  55.  
  56.     private Light LightSource => GetComponent<Light>();
  57.  
  58.     private void OnValidate()
  59.     {
  60.         if (maxIntensity < minIntensity) minIntensity = maxIntensity - 0.5f;
  61.         if (maxRange < minRange) minRange = maxRange - 0.5f;
  62.  
  63.         if (maxFlickerFrequency < minFlickerFrequency) minFlickerFrequency = maxFlickerFrequency;
  64.  
  65.         LightSource.color = colorA;
  66.         LightSource.range = maxRange;
  67.  
  68.         if(castShadows)
  69.         {
  70.             if (LightSource.type == UnityEngine.LightType.Point)
  71.                 LightSource.bounceIntensity = 0;
  72.         }
  73.     }
  74.  
  75.     private void Awake()
  76.     {
  77.         lPos = transform.position;
  78.         curPos = lPos;
  79.         timeOfLastFlicker = Time.time;
  80.     }
  81.  
  82.     private void Update()
  83.     {
  84.         if (timeOfLastFlicker + flickerFrequency < Time.time)
  85.         {
  86.             timeOfLastFlicker = Time.time;
  87.            
  88.             nextIntensity = Random.Range(minIntensity, maxIntensity);
  89.             nextRange = Random.Range(minRange, maxRange);
  90.             newPos = MaxUtilities.RandomPointInBox(lPos, new Vector3(ligthDisplacement, ligthDisplacement, ligthDisplacement));
  91.            
  92.             flickerFrequency = Random.Range(minFlickerFrequency, maxFlickerFrequency);
  93.  
  94.         }
  95.  
  96.         Flicker();
  97.     }
  98.  
  99.     private void Flicker()
  100.     {
  101.         LightSource.intensity = Mathf.Lerp(LightSource.intensity, nextIntensity, smoothness * Time.deltaTime);
  102.         LightSource.range = Mathf.Lerp(LightSource.range, nextRange, smoothness * Time.deltaTime);
  103.  
  104.         LightSource.color = Color.Lerp(colorA, colorB, Mathf.PingPong(Time.time, 1));
  105.  
  106.         transform.position = Vector3.Lerp(curPos, newPos, smoothness * Time.deltaTime);
  107.         curPos = transform.position;
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement