Advertisement
GaelVanhalst

Bunny Annah Stones: Player Light

Jan 25th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerLight : MonoBehaviour
  4. {
  5.     [SerializeField]private float _distanceLightGoesOn = -10.0f;
  6.     [SerializeField]private float _distanceLightIsMax = 200.0f;
  7.     [SerializeField]private float _lightMaxBrightness = 2.0f;
  8.     [SerializeField]private float _flickerSpeed = 1.0f;
  9.     [SerializeField] private float _flickerSize = 0.25f;
  10.     [SerializeField] private Light _light = null;
  11.     private float _lightOffsetNoise = 0;
  12.  
  13.     void Update ()
  14.     {
  15.         var depthForLight = transform.position.y;
  16.         depthForLight += _distanceLightGoesOn;
  17.         depthForLight *= -1;
  18.  
  19.         //Calculate if light should be on
  20.         var lightShouldBeOn = depthForLight > 0;
  21.         if (!lightShouldBeOn)
  22.         {
  23.             _light.enabled = false;
  24.         }
  25.         else
  26.         {
  27.             _light.enabled = true;
  28.  
  29.             //Calculate depth for the intensity
  30.             depthForLight /= (_distanceLightIsMax - _distanceLightGoesOn);
  31.             depthForLight = Mathf.Min(depthForLight, 1);
  32.  
  33.             //Calculate the flicker amount
  34.             _lightOffsetNoise += Time.deltaTime*_flickerSpeed;
  35.             var flickerDecreaseAmount = 1-((1 - Mathf.PerlinNoise(_lightOffsetNoise, 0))*_flickerSize);
  36.              
  37.             //Calculate final intensity
  38.             _light.intensity = _lightMaxBrightness* depthForLight* flickerDecreaseAmount;
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement