当我移动游戏时,不是完全播放声音,而是循环播放几毫秒。
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Footsteps : MonoBehaviour
{
public AudioSource audioSource;
public PlayerMovement pM;
void Start()
{
audioSource.Stop();
}
void Update()
{
PlaySound();
}
public void PlaySound()
{
if (Input.GetKey(KeyCode.W))
{
audioSource.Play();
}
else
{
audioSource.Stop();
}
}
}
视频Example。
任何建议将不胜感激!
最佳答案
只要按下键,就会调用Input.GetKey()
方法。为此,您可以使用Input.GetKeyDown()
这样的示例:
public void PlaySound()
{
if (Input.GetKeyDown(KeyCode.W))
{
audioSource.Play();
}
else if(Input.GetKeyUp(KeyCode.W))
{
audioSource.Stop();
}
}
还有其他方法可以产生这种声音,但是这种简单的方法应该可以工作。
引用文献:
https://docs.unity3d.com/ScriptReference/Input.GetKey.html
https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
关于c# - Unity 3D:脚步声在最初的几毫秒内循环播放,而不是播放完整的声音然后循环播放,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62227947/