Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma strict
- public var flashlight : Light; // The flashlight GameObject (must have Light component)
- public var switchOn : AudioClip; // Switch on sound
- public var switchOff : AudioClip; // Switch off sound
- public var isOn : boolean = false; // Is the light on?
- public var alpha : float; // Flashlight color
- public var charge : float; // Battery charge speed
- public var curPower : float; // Current battery power
- public var maxPower : float; // Maximum battery power
- public var flickerTime : float; // Flashlight flicker threshold (percent until flashlight flickers)
- public var flickerDuration : float; // Time between each flicker
- private var _isBatteryFull : boolean; // Is the battery fully charged?
- private var _defaultIntensity : float; // Reference to the flashlight intensity
- /// <summary> Turn off/on the flashlight (When a key is pressed). </summary>
- public function UseFlashlight()
- {
- // Toggle flashlight? (Pressed "F" key)
- if ( Input.GetKeyDown( KeyCode.F ) )
- {
- ToggleFlashlight();
- Debug.Log( "Flashlight On: " + isOn );
- }
- // Is flashlight on?
- if ( isOn == true )
- {
- // Use a function to drain /decrease the battery power over time
- DrainBattery ();
- }
- // Battery full?
- else if ( _isBatteryFull == false )
- {
- // Use a function to charge/increase the battery power over time
- ChargeBattery();
- }
- }
- /// <summary> Drain the flashlight battery (While in use). </summary>
- public function DrainBattery()
- {
- _isBatteryFull = false;
- // Battery active?
- if ( curPower > 0.0 )
- {
- Debug.Log( "Battery in use..." );
- curPower -= Time.deltaTime * charge;
- Debug.Log( "Battery power: " + curPower );
- // Flicker?
- if ( curPower < maxPower / flickerTime )
- {
- FlickerFlashlight();
- }
- }
- else
- {
- // Once the battery is dead, automatically toggle the light off
- ToggleFlashlight();
- }
- }
- /// <summary> Charge the flashlight battery. </summary>
- public function ChargeBattery()
- {
- // Charged?
- if ( curPower >= maxPower )
- {
- curPower = maxPower;
- _isBatteryFull = true;
- return;
- }
- Debug.Log( "Charging battery..." );
- // Flicker?
- if ( curPower < maxPower / flickerTime )
- {
- // Fade the color back to white if within flickerTime threshold
- FadeFlashlight();
- }
- else
- {
- // Once the flashlight passes the flickerTime threshold, its color will reset to full
- SetFlashlight( 1.0 );
- }
- curPower += Time.deltaTime * ( charge / 2 );
- Debug.Log( "Battery power: " + curPower );
- }
- /// <summary> Turn on the flashlight. </summary>
- public function FlashlightOn()
- {
- isOn = true;
- flashlight.enabled = isOn;
- }
- /// <summary> Turn off the flashlight. </summary>
- public function FlashlightOff()
- {
- isOn = false;
- flashlight.enabled = isOn;
- }
- /// <summary> Toggle the flashlight (Turn on-and-off). </summary>
- public function ToggleFlashlight()
- {
- // If isOn is true, it will be flipped to false and vice-versa.
- isOn = !isOn;
- // Set the flashlight to your flag.
- flashlight.enabled = isOn;
- }
- /// <summary> Fade the flashlight color. </summary>
- public function FadeFlashlight()
- {
- alpha = curPower / maxPower;
- flashlight.color = Color( alpha, alpha, alpha, alpha );
- }
- /// <summary> Set the flashlight color. </summary>
- public function SetFlashlight( value : float )
- {
- alpha = value;
- flashlight.color = Color( alpha, alpha, alpha, alpha );
- }
- /// <summary> Flicker the flashlight. </summary>
- public function FlickerFlashlight()
- {
- FadeFlashlight();
- var phi : float = Time.time / flickerDuration;
- var amplitude : float = Mathf.Cos( phi ) * 0.5 + _defaultIntensity;
- flashlight.intensity = amplitude + Random.Range( 0.1, 1.0 );
- }
- function Start()
- {
- // In my opinion, it is better to use a public variable and drag the flashlight
- // inside the slot in the inspector so as to avoid searching for the gameobject.
- // In case you forget to do that, here we check to see
- // if there is a light and search for it if necessary.
- if ( flashlight == null )
- {
- Debug.LogWarning( "There is no flashlight GameObject attached to this component! Searching scene now..." );
- // OPTION 1 - Using Find/FindWithTag
- // GameObject.FindWithTag is less expensive to use than
- // GameObject.Find but its use is not recommended every frame.
- flashlight = GameObject.FindWithTag( "Flashlight" ).GetComponent( Light );
- // OPTION 2 - Better choice
- // Instead of searching for the GameObject, this script should
- // be attached to the actual flashlight scene object. Then you
- // can reference this GameObject if the flashlight is null
- flashlight = GetComponent( Light );
- }
- flashlight.enabled = isOn;
- _defaultIntensity = flashlight.intensity;
- curPower = maxPower;
- }
- function Update()
- {
- UseFlashlight();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement