Last active
May 28, 2022 13:18
-
-
Save Mazday21/3de1f3e9aca9eb0f08b040d82874cc9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[RequireComponent(typeof(AudioSource))] | |
public class Signaling : MonoBehaviour | |
{ | |
[SerializeField] private SpriteRenderer _sprite; | |
private bool IsEnter = false; | |
private AudioSource _alarm; | |
private void OnTriggerEnter2D(Collider2D collision) | |
{ | |
if (collision.TryGetComponent<Player>(out Player player)) | |
{ | |
if (!IsEnter) | |
{ | |
_alarm.Play(); | |
IsEnter = true; | |
_sprite.color = Color.black; | |
} | |
else | |
{ | |
IsEnter = false; | |
_sprite.color = Color.white; | |
} | |
} | |
} | |
private void Awake() | |
{ | |
_alarm = GetComponent<AudioSource>(); | |
} | |
private void Start() | |
{ | |
StartCoroutine(SoundChanger()); | |
} | |
private IEnumerator SoundChanger() | |
{ | |
var waitForSecond = new WaitForSeconds(0.1f); | |
float volume = 0; | |
while (IsEnter && volume < 1f) | |
{ | |
volume += 0.1f; | |
_alarm.volume = volume; | |
yield return waitForSecond; | |
} | |
while (!IsEnter && volume > 0) | |
{ | |
volume -= 0.1f; | |
_alarm.volume = volume; | |
if(_alarm.volume < 0.1f) | |
{ | |
_alarm.Stop(); | |
} | |
yield return waitForSecond; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment