Advertisement
ReflectedMirror

Flash light script

Aug 16th, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2.  
  3. //Variables START
  4.  
  5. //Light Source && Battery Life
  6. var flashlightLightSource : Light;
  7. var lightOn : boolean = true;
  8. var lightDrain : float = 0.1;
  9. var batteryLife : float = 0.0;
  10. var maxBatteryLife : float = 2.0;
  11.  
  12. //Sound Clips
  13. //var soundTurnOn : AudioClip;
  14. //var soundTurnOff : AudioClip;
  15.  
  16. //Variables END
  17.  
  18. function Start()
  19. {
  20.         batteryLife = maxBatteryLife;
  21.         flashlightLightSource = GetComponent(Light);
  22. }
  23.  
  24. function Update()
  25. {
  26.         if(lightOn && batteryLife >= 0)
  27.         {
  28.                 batteryLife -= Time.deltaTime * lightDrain;
  29.         }
  30.  
  31.         if(batteryLife <= 0)
  32.         {
  33.                 batteryLife = 0;
  34.                 lightOn = false;
  35.         }
  36.        
  37.         if(batteryLife <= 1)
  38.         {
  39.             flashlightLightSource.light.intensity = 0.6;
  40.         }
  41.        
  42.         if(batteryLife <= 0.5)
  43.         {
  44.             flashlightLightSource.light.intensity = 0.3;
  45.         }
  46.        
  47.         if(Input.GetKeyUp(KeyCode.F))
  48.         {
  49.                 toggleFlashlight();
  50.                // toggleFlashlightSFX();
  51.                
  52.                 if(lightOn)
  53.                 {
  54.                         lightOn = false;
  55.                 }
  56.                 else if(!lightOn && batteryLife >= 0)
  57.                 {
  58.                         lightOn = true;
  59.                 }
  60.         }
  61. }
  62.  
  63. function toggleFlashlight()
  64. {
  65.         if(lightOn)
  66.         {
  67.                 flashlightLightSource.enabled = false;
  68.         }
  69.         else
  70.         {
  71.                 flashlightLightSource.enabled = true;
  72.         }
  73. }
  74.  
  75. /*function toggleFlashlightSFX()
  76. {
  77.         if(flashlightLightSource.enabled)
  78.         {
  79.                 audio.clip = soundTurnOn;
  80.         }
  81.         else
  82.         {
  83.                 audio.clip = soundTurnOff;
  84.         }
  85.        
  86.         audio.Play();
  87. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement