C# Unity3d-播放和暂停之间转换时的音频粗糙声音
我有一个统一的脚本,可以在播放器移动时播放音频(脚步)。脚本正在运行,但还有一个问题,在播放和暂停之间转换时,音频似乎太粗糙(可能会伤害我的演讲者呵呵) 这是我的剧本C# Unity3d-播放和暂停之间转换时的音频粗糙声音,c#,unity3d,audio,unity3d-5,C#,Unity3d,Audio,Unity3d 5,我有一个统一的脚本,可以在播放器移动时播放音频(脚步)。脚本正在运行,但还有一个问题,在播放和暂停之间转换时,音频似乎太粗糙(可能会伤害我的演讲者呵呵) 这是我的剧本 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput; public class PlayerController : MonoBeha
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerController : MonoBehaviour {
Rigidbody playerRigid;
private Vector3 pindah;
public AudioSource audioSource;
public AudioClip audioClip;
public float lowPitchRange = .95f;
public float hiPitchRange = 1.05f;
void Awake () {
audioSource = GetComponent<AudioSource> ();
audioSource.clip = audioClip;
float randomPitch = Random.Range (lowPitchRange, hiPitchRange);
audioSource.pitch = randomPitch;
}
// Use this for initialization
void Start () {
//mengambil komponen rigidbody player
playerRigid = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
//untuk mendapatkan input dari virtual joystick button
float h = CrossPlatformInputManager.GetAxisRaw ("Horizontal");
float v = CrossPlatformInputManager.GetAxisRaw ("Vertical");
if ((h != 0f || v != 0f) && !audioSource.isPlaying) {
audioSource.Play ();
} else {
audioSource.Pause ();
}
//memindah posisi player sesuai input yang didapat
pindah = new Vector3 (h, 0.0f, v);
playerRigid.velocity = pindah;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityStandardAssets.CrossPlatformInput;
公共类玩家控制器:单行为{
刚体playerRigid;
私人矢量3平达;
公共音频源;
公共音频剪辑;
公众浮标低量程=.95f;
公共浮动范围=1.05f;
无效唤醒(){
audioSource=GetComponent();
audioSource.clip=音频剪辑;
浮动随机俯仰=随机范围(低俯仰范围、高俯仰范围);
audioSource.pitch=随机音高;
}
//用于初始化
无效开始(){
//门甘比尔·科波宁刚体运动员
playerRigid=GetComponent();
}
//每帧调用一次更新
无效更新(){
//取消输入dari虚拟操纵杆按钮
float h=CrossPlatformInputManager.GetAxisRaw(“水平”);
float v=CrossPlatformInputManager.GetAxisRaw(“垂直”);
如果((h!=0f | | v!=0f)和&!audioSource.isPlaying){
Play();
}否则{
暂停();
}
//梅明达·波西西球员塞苏艾输入杨·迪达帕
pindah=新矢量3(h,0.0f,v);
playerRigid.velocity=品达;
}
}
我想您需要像这样更改代码:
if (h != 0f || v != 0f) {
if (!audioSource.isPlaying) audioSource.Play ();
} else {
audioSource.Pause ();
}
我想您需要像这样更改代码:
if (h != 0f || v != 0f) {
if (!audioSource.isPlaying) audioSource.Play ();
} else {
audioSource.Pause ();
}
如果问题是音频源在暂停时弹出,也许你应该考虑用这个类似的东西来淡出音量(我没有测试过我自己,我稍后再试试):< /P>
//淡入淡出/增加时间(秒)
公共浮动汇率时间=1;
公共FadeSound(){
//Play();
开始例行程序(_FadeSound);
}
公共增量声音(){
启动例行程序(增加声音);
}
IEnumerator_FadeSound(){
浮动t=速率时间;
而(t>0){
收益返回空;
t-=时间增量时间;
source.volume=t/费率时间;
}
屈服断裂;
//产生音频源。暂停();
}
IEnumerator_IncreaseSound(){
浮动t=0;
而(t<1){
收益返回空;
t+=时间增量时间;
source.volume=t/费率时间;
}
屈服断裂;
}
如果((h!=0f | | v!=0f)和&!audioSource.isPlaying){
增加声音();
}否则{
FadeSound();
}
>P>如果问题是音频源在暂停时弹出,也许你应该考虑用类似的东西来淡出音量(我没有测试过我自己,我稍后再试试):
//淡入淡出/增加时间(秒)
公共浮动汇率时间=1;
公共FadeSound(){
//Play();
开始例行程序(_FadeSound);
}
公共增量声音(){
启动例行程序(增加声音);
}
IEnumerator_FadeSound(){
浮动t=速率时间;
而(t>0){
收益返回空;
t-=时间增量时间;
source.volume=t/费率时间;
}
屈服断裂;
//产生音频源。暂停();
}
IEnumerator_IncreaseSound(){
浮动t=0;
而(t<1){
收益返回空;
t+=时间增量时间;
source.volume=t/费率时间;
}
屈服断裂;
}
如果((h!=0f | | v!=0f)和&!audioSource.isPlaying){
增加声音();
}否则{
FadeSound();
}
通常,最好从编辑参数的当前值开始这种平滑过渡,在这种情况下,即source.volume。否则,当音量瞬间跳到过渡的起始值(0或1)时,如果在FadeSound和IncreaseSound之间切换过快,就会出现pops。在开始新的协同程序之前,您可能还需要停止任何冲突的协同程序。谢谢您的反馈,matti,我将在今天下午更新答案。我觉得这不是用户所期望的,但无论如何它对我来说都是有用的。谢谢通常,从编辑参数的当前值开始这种平滑过渡是个好主意,在这种情况下,即source.volume。否则,当音量瞬间跳到过渡的起始值(0或1)时,如果在FadeSound和IncreaseSound之间切换过快,就会出现pops。在开始新的协同程序之前,您可能还需要停止任何冲突的协同程序。谢谢您的反馈,matti,我将在今天下午更新答案。我觉得这不是用户所期望的,但无论如何它对我来说都是有用的。谢谢