Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- /// <ToDo>
- /// IMPLEMENT PARAMETERS FOR DIFFERENT LIGHT
- /// </ToDo>
- enum EmitterType
- {
- candle,
- fire,
- bulb,
- neon
- }
- [RequireComponent(typeof(Light))]
- public class FireLight : MonoBehaviour
- {
- #region Variables
- [Tooltip("Maximum possible intensity the light can flicker to")]
- [SerializeField, Min(0)] float maxIntensity = 1.5f;
- [Tooltip("Minimum possible intensity the light can flicker to")]
- [SerializeField, Min(0)] float minIntensity = 0.5f;
- [Space]
- [Tooltip("Maximum light range")]
- [SerializeField] float maxRange = 5.7f;
- [Tooltip("Minimum light range")]
- [SerializeField] float minRange = 0.5f;
- [Space]
- [Tooltip("How much the light will move around to have a more visible fire effect")]
- [SerializeField] float ligthDisplacement = 0.25f;
- [Space]
- [SerializeField] Color colorA = new Color(227, 173, 78);
- [SerializeField] Color colorB = new Color(227, 139, 78);
- [Space]
- [Tooltip("how fast the light will flicker: Low (3 or 5) Fire or Candle - Hight: dying Neon")]
- [SerializeField, Range(0, 50)] float smoothness = 3f;
- [Space]
- [Tooltip("If true, will set the light to cast shadows by changing the LightSource parameters")]
- [SerializeField] bool castShadows = false;
- float maxFlickerFrequency = 1f;
- float minFlickerFrequency = 0.1f;
- float nextIntensity;
- float nextRange;
- float flickerFrequency;
- float timeOfLastFlicker;
- Vector3 lPos,curPos, newPos;
- #endregion
- private Light LightSource => GetComponent<Light>();
- private void OnValidate()
- {
- if (maxIntensity < minIntensity) minIntensity = maxIntensity - 0.5f;
- if (maxRange < minRange) minRange = maxRange - 0.5f;
- if (maxFlickerFrequency < minFlickerFrequency) minFlickerFrequency = maxFlickerFrequency;
- LightSource.color = colorA;
- LightSource.range = maxRange;
- if(castShadows)
- {
- if (LightSource.type == UnityEngine.LightType.Point)
- LightSource.bounceIntensity = 0;
- }
- }
- private void Awake()
- {
- lPos = transform.position;
- curPos = lPos;
- timeOfLastFlicker = Time.time;
- }
- private void Update()
- {
- if (timeOfLastFlicker + flickerFrequency < Time.time)
- {
- timeOfLastFlicker = Time.time;
- nextIntensity = Random.Range(minIntensity, maxIntensity);
- nextRange = Random.Range(minRange, maxRange);
- newPos = MaxUtilities.RandomPointInBox(lPos, new Vector3(ligthDisplacement, ligthDisplacement, ligthDisplacement));
- flickerFrequency = Random.Range(minFlickerFrequency, maxFlickerFrequency);
- }
- Flicker();
- }
- private void Flicker()
- {
- LightSource.intensity = Mathf.Lerp(LightSource.intensity, nextIntensity, smoothness * Time.deltaTime);
- LightSource.range = Mathf.Lerp(LightSource.range, nextRange, smoothness * Time.deltaTime);
- LightSource.color = Color.Lerp(colorA, colorB, Mathf.PingPong(Time.time, 1));
- transform.position = Vector3.Lerp(curPos, newPos, smoothness * Time.deltaTime);
- curPos = transform.position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement