Advertisement
OwlyOwl

dz_alarm

Apr 24th, 2020
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(AudioSource))]
  6.  
  7. public class Alarm : MonoBehaviour
  8. {
  9.     public AudioSource AlarmSound;
  10.     [SerializeField] private AudioSource _audiosource;
  11.     private bool _volFull = false;
  12.  
  13.     private void Awake()
  14.     {
  15.         AlarmSound = GetComponent<AudioSource>();
  16.         AlarmSound.volume = 0.01f;
  17.     }
  18.  
  19.     private void OnTriggerEnter()
  20.     {
  21.         AlarmSound.Play();
  22.     }
  23.  
  24.     private void OnTriggerExit()
  25.     {
  26.         AlarmSound.Stop();
  27.     }
  28.  
  29.     private void Update()
  30.     {
  31.         float elapsedTime = Time.deltaTime;
  32.         if (AlarmSound.volume <= 1 && _volFull == false)
  33.         {
  34.             AlarmSound.volume += elapsedTime;
  35.             if (AlarmSound.volume >= 0.99f)
  36.             {
  37.                 _volFull = true;
  38.             }
  39.         }
  40.         if (_volFull == true)
  41.         {
  42.             AlarmSound.volume -= elapsedTime;
  43.             if (AlarmSound.volume == 0f)
  44.             {
  45.                 _volFull = false;
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement