Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlayerLight : MonoBehaviour
- {
- [SerializeField]private float _distanceLightGoesOn = -10.0f;
- [SerializeField]private float _distanceLightIsMax = 200.0f;
- [SerializeField]private float _lightMaxBrightness = 2.0f;
- [SerializeField]private float _flickerSpeed = 1.0f;
- [SerializeField] private float _flickerSize = 0.25f;
- [SerializeField] private Light _light = null;
- private float _lightOffsetNoise = 0;
- void Update ()
- {
- var depthForLight = transform.position.y;
- depthForLight += _distanceLightGoesOn;
- depthForLight *= -1;
- //Calculate if light should be on
- var lightShouldBeOn = depthForLight > 0;
- if (!lightShouldBeOn)
- {
- _light.enabled = false;
- }
- else
- {
- _light.enabled = true;
- //Calculate depth for the intensity
- depthForLight /= (_distanceLightIsMax - _distanceLightGoesOn);
- depthForLight = Mathf.Min(depthForLight, 1);
- //Calculate the flicker amount
- _lightOffsetNoise += Time.deltaTime*_flickerSpeed;
- var flickerDecreaseAmount = 1-((1 - Mathf.PerlinNoise(_lightOffsetNoise, 0))*_flickerSize);
- //Calculate final intensity
- _light.intensity = _lightMaxBrightness* depthForLight* flickerDecreaseAmount;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement