如何解决如何在IF语句中以一定值播放声音? Unity C#
如何在不同的if语句中播放随机声音? 例如,当值在0到25之间时,然后播放一次或两次声音,当值(我的滑块)在25到40之间时,在该行中播放更多声音...我想模拟呼吸,但是我认为此脚本太多了为我处理。
声音仅播放一次,并且仅在我开始游戏时播放。 看起来Unity只能先读取If语句,然后再什么也不会发生。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayingSoundsystem: MonoBehaviour {
private AudioSource breath_sound;
[SerializeField]
private AudioClip[] breath_Clip;
private AudioClip breath;
public float volume_Min,volume_Max;
public GameObject Textvalue;
public Slider mainSlider;
public GameObject Spotlight;
public GameObject blur;
private void Start() {
breath_sound = GetComponent<AudioSource>();
}
private void Update() {
Textvalue.GetComponent<Text>().text = mainSlider.value.ToString();
if (Spotlight.GetComponent<Light>().enabled == false) {
mainSlider.value += 1*Time.deltaTime;
}
else {
mainSlider.value -= 0.5f * Time.deltaTime;
}
if (mainSlider.value <= 25) {
breath_sound.volume = Random.Range(volume_Min,volume_Max);
breath_sound.clip = breath_Clip[Random.Range(0,breath_Clip.Length)];
breath_sound.PlayOneShot(breath_sound.clip);
//Play Random Sound from 13 different one's
}
if (mainSlider.value > 25 && mainSlider.value <= 40) {
int index = Random.Range(0,breath_Clip.Length);
breath = breath_Clip[index];
breath_sound.clip = breath;
breath_sound.Play();
}
if (mainSlider.value > 40 && mainSlider.value <= 60) {
breath_sound.volume = Random.Range(volume_Min,breath_Clip.Length)];
breath_sound.PlayOneShot(breath_sound.clip);
}
if (mainSlider.value > 60 && mainSlider.value <= 85) {
breath_sound.volume = Random.Range(volume_Min,breath_Clip.Length)];
breath_sound.PlayOneShot(breath_sound.clip);
}
if (mainSlider.value > 85 && mainSlider.value < 100) {
breath_sound.volume = Random.Range(volume_Min,breath_Clip.Length)];
breath_sound.PlayOneShot(breath_sound.clip);
}
if (mainSlider.value == 100) {
}
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag ("Jumpscare")) {
mainSlider.value += 5;
}
}
public void SubmitSliderSetting() {
Debug.Log(mainSlider.value);
}
}
解决方法
首先,如果要重复声音,则需要确保在这些音频源上启用了循环。
现在,我们不需要在每一帧都调用声音,而是只要更改了主滑块的值就可以调用它们。
要实现此目的,我们首先需要在Start方法中从mainSlider中获取currentValue,然后只需检查Update方法中的值是否仍等于该当前Value或它是否已更改。
检测值更改:
[SerializeField]
private AudioClip[] breath_Clip;
private AudioSource breath_sound;
private float currentValue = 0f;
private Text sliderText;
private Light spotLight;
private void Start() {
// Get AudioSource of this component
breath_sound = GetComponent<AudioSource>();
// Getting start sliderValue
currentValue = mainSlider.value;
// Getting refernce to the Text Component of Textvalue.
sliderText = Textvalue.GetComponent<Text>();
// Getting reference to the Light component of SpotLight.
spotLight = Spotlight.GetComponent<Light>();
// Setting text equal to the start slider Value
sliderText.text = mainSlider.value.ToString();
}
private void Update() {
// Update value when the light is deactivated/activated
if (spotlight.enabled == false) {
mainSlider.value += Time.deltaTime;
}
else {
mainSlider.value -= 0.5f * Time.deltaTime;
}
// Getting current sliderValue
float value = mainSlider.value;
// Check if the sliderValue has changed
if (currentValue != value) {
// Set currentValue to the correct Value
currentValue = value;
ValueChangedCallback(value);
}
}
现在,我们已经创建了对mainSlider值所做的更改的检测,我们可以使函数根据当前的滑块值来更改audioClip和文本。
ValueChangedCallback方法:
private void ValueChangedCallback(float value) {
// Setting text equal to the current slider Value
sliderText.text = value.ToString();
// Define clip that should be played (default is 0)
int clipIndex = 0;
// Define volume clip should be played at (increase when the slider Value is higher)
float volume = 10f;
// Play different breath track depending on current slider Value
if (value <= 25) {
volume = 10f;
clipIndex = 0;
}
else if (value > 25 && value <= 40) {
volume = 15f;
clipIndex = 1;
}
// Do the same for all the other possibilities ...
breath_sound.volume = volume;
breath_sound.PlayOneShot(breath_Clip[clipIndex]);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。