Search Unity

FlashLight/pick up script support

Discussion in 'Scripting' started by Luke3671, Feb 22, 2016.

  1. Luke3671

    Luke3671

    Joined:
    Jul 6, 2014
    Posts:
    56
    Hello, Not sure if anyone can help me or not what i'm having problem is following:

    FlashLight.js

    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4. var flashlightLightSource : Light;
    5. var lightOn : boolean = true;
    6. var lightDrain : float = 0.1;
    7. private static var batteryLife : float = 0.0;
    8. var maxBatteryLife : float = 2.0;
    9.  
    10. private static var batteryPower : float = 1;
    11.  
    12. var barDisplay : float = 0;
    13. var pos : Vector2 = new Vector2(20,40);
    14. var size : Vector2 = new Vector2(60,20);
    15. var progressBarEmpty : Texture2D;
    16. var progressBarFull : Texture2D;
    17.  
    18. var soundTurnOn : AudioClip;
    19. var soundTurnOff : AudioClip;
    20.  
    21.  
    22. function Start()
    23. {
    24.     batteryLife = maxBatteryLife;
    25.     flashlightLightSource = GetComponent(Light);
    26. }
    27.  
    28.  
    29. static function AlterEnergy (amount : int)
    30. {
    31.     batteryLife = Mathf.Clamp(batteryLife+batteryPower, 0, 100);
    32.  
    33. }
    34.  
    35.  
    36.  
    37. function Update()
    38. {
    39.  
    40. //BATTERY LIFE BRIGHTNESS//////////
    41.     if(lightOn && batteryLife >= 0)
    42.     {
    43.         batteryLife -= Time.deltaTime * lightDrain;
    44.     }
    45.         if(lightOn && batteryLife <= 0.4)
    46.     {
    47.         flashlightLightSource.GetComponent.<Light>().intensity = 5;
    48.     }
    49.         if(lightOn && batteryLife <= 0.3)
    50.     {
    51.         flashlightLightSource.GetComponent.<Light>().intensity = 4;
    52.     }
    53.     if(lightOn && batteryLife <= 0.2)
    54.     {
    55.         flashlightLightSource.GetComponent.<Light>().intensity = 3;
    56.     }
    57.         if(lightOn && batteryLife <= 0.1)
    58.     {
    59.         flashlightLightSource.GetComponent.<Light>().intensity = 2;
    60.     }
    61.             if(lightOn && batteryLife <= 0)
    62.     {
    63.         flashlightLightSource.GetComponent.<Light>().intensity = 0;
    64.     }
    65.    
    66.  
    67.    
    68.     barDisplay = batteryLife;
    69.    
    70.     if(batteryLife <= 0)
    71.     {
    72.         batteryLife = 0;
    73.         lightOn = false;
    74.     }
    75.    
    76.     if(Input.GetKeyUp(KeyCode.F))
    77.     {
    78.         toggleFlashlight();
    79.         toggleFlashlightSFX();
    80.        
    81.         if(lightOn)
    82.         {
    83.             lightOn = false;
    84.         }
    85.         else if (!lightOn && batteryLife >= 0)
    86.         {
    87.             lightOn = true;
    88.         }
    89.     }
    90. }
    91.    
    92.     /////// PIC  ///////////
    93. function OnGUI()
    94. {
    95.  
    96.     // draw the background:
    97.     GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
    98.         GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
    99.  
    100.         // draw the filled-in part:
    101.         GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
    102.             GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
    103.         GUI.EndGroup ();
    104.  
    105.     GUI.EndGroup ();
    106.  
    107. }
    108.    
    109. function toggleFlashlight()
    110. {
    111.     if(lightOn)
    112.     {
    113.         flashlightLightSource.enabled = false;
    114.     }
    115.     else
    116.     {
    117.         flashlightLightSource.enabled = true;
    118.     }
    119. }
    120. function toggleFlashlightSFX()
    121. {
    122.     if(flashlightLightSource.enabled)
    123.     {
    124.         GetComponent.<AudioSource>().clip = soundTurnOn;
    125.     }
    126.     else
    127.     {
    128.         GetComponent.<AudioSource>().clip = soundTurnOff;
    129.     }
    130.     GetComponent.<AudioSource>().Play();
    131.    
    132. }
    133.  
    134.     @script RequireComponent(AudioSource)
    135.    
    That the flashlight script

    This is the battery.js

    Code (JavaScript):
    1. var buttonInRange;
    2. var buttonActivated;
    3. var batterySound : AudioClip;
    4. private static var batteryPower : float = 10;
    5.  
    6. public var guiSkin : GUISkin;
    7.  
    8. private var hasPlayed = false;
    9.  
    10. function OnTriggerEnter (c : Collider)
    11. {
    12.     buttonInRange = true;
    13.  
    14. }
    15. function OnTriggerExit (c : Collider)
    16. {
    17.     buttonInRange = false;
    18.  
    19. }
    20. function OnGUI ()
    21. {
    22.     if(buttonInRange == true)
    23.     {
    24.         GUI.skin = guiSkin;
    25.         GUI.Label (Rect (Screen.width/2-50, Screen.height/2-55, 120, 50), "Pick up batteries");
    26.    
    27.     }
    28.  
    29. }
    30. function Update ()
    31. {
    32.     if (buttonInRange == true)
    33.     {
    34.         if (Input.GetKeyDown ("e"))
    35.         {
    36.             if(!hasPlayed)
    37.             {
    38.                 AudioSource.PlayClipAtPoint(batterySound, transform.position);
    39.                 FlashLight.AlterEnergy(batteryPower);
    40.                 Destroy(gameObject);
    41.                 hasPlayed = true;
    42.            
    43.             }
    44.        
    45.         }
    46.    
    47.     }
    48.  
    49. }
    Problem i'm having with this is when pick up "item" The values go up correct but light doesn't turn on till half empty, Been looking into it but can't find the problem at all, Does anyone see the error making this happen?

    Thank you
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    first problem is that your "AlterEnergy" function doesn't use it's parameter, it's using the local variable in the flashlight script.

    secondly you're setting the "barDisplay" before zeroing the batteryLife below zero, so in some cases the fill amount will be scaling the wrong way.

    lastly, once the battery has been emptied there is no line of code to tell the intensity to change until it's below 0.4. i.e. line 63 is called, then batterylife is 1, then follow through the logic again, no intensity changes.


    lastly, why are you using the old clunky OnGUI? it was replaced for in game UI in unity 4.6.
    https://unity3d.com/learn/tutorials/topics/user-interface-ui