Advertisement
Diamond32_Tutoriales

LinternaPro

May 16th, 2021
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Linterna : MonoBehaviour
  6. {
  7.     public Light luz;
  8.     AudioSource sm;
  9.     public AudioClip sound;
  10.     public GameObject LinternaOBJ;
  11.  
  12.     [Range (0, 100)]public float CurBateria;
  13.     public int Baterias;
  14.     public bool isOn;
  15.     [Range(0, 100)] public float Rbateria;
  16.     public bool BatteryLow;
  17.  
  18.     bool wait;
  19.  
  20.     void Start()
  21.     {
  22.         sm = gameObject.AddComponent<AudioSource>();
  23.     }
  24.  
  25.     void Update()
  26.     {
  27.         if (Input.GetKeyDown(KeyCode.F) && LinternaOBJ.active == true)
  28.         {
  29.             sm.PlayOneShot(sound, 1f);
  30.             isOn = !isOn;
  31.         }
  32.  
  33.         if (CurBateria <= 15f && Baterias > 0 && Input.GetKeyDown (KeyCode.R))
  34.         {
  35.             CurBateria += 100;
  36.             Baterias--;
  37.         }
  38.  
  39.         if (CurBateria <= 10f)
  40.         {
  41.             BatteryLow = true;
  42.  
  43.             if (!wait)
  44.             {
  45.                 StartCoroutine(parpadeo());
  46.                 wait = true;
  47.             }
  48.         }
  49.         else
  50.         {
  51.             BatteryLow = false;
  52.         }
  53.  
  54.             if (isOn)
  55.             {
  56.                 CurBateria -= Rbateria * Time.fixedDeltaTime;
  57.             }
  58.        
  59.         if (CurBateria <= 0)
  60.         {
  61.             isOn = false;
  62.         }
  63.  
  64.         if (!wait) {
  65.             luz.enabled = isOn;
  66.         }
  67.  
  68.         CurBateria = Mathf.Clamp (CurBateria, 0, 100);
  69.     }
  70.  
  71.     IEnumerator parpadeo()
  72.     {
  73.         if (BatteryLow)
  74.         {
  75.             luz.enabled = false;
  76.             yield return new WaitForSeconds(0.8f);
  77.             luz.enabled = true;
  78.             wait = false;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement