C# 为什么即使我';我在使用协同程序? void Update() { isground=physical.CheckSphere(groundCheck.position、groundDistance、groundMask); x=输入。GetAxis(“水平”); z=输入。GetAxis(“垂直”); start例程(footprintsounds()); } IEnumerator声音() { 如果(接地) { 如果(x>0 | | z>0) { //试图颠倒顺序,但仍然没有结果 返回新的等待秒(1f); FindObjectOfType().Play(“Foot”); } } }
这是从我的脚本中提取的一段代码。 我曾希望,因为我正在使用一个协同程序,它会等待WaitForSeconds的间隔,然后再次播放,但它只是无限重复,好像没有计时器或任何东西 你能帮我弄清楚为什么这个不能运行吗? 关于如何编写足迹脚本的提示也将非常感谢C# 为什么即使我';我在使用协同程序? void Update() { isground=physical.CheckSphere(groundCheck.position、groundDistance、groundMask); x=输入。GetAxis(“水平”); z=输入。GetAxis(“垂直”); start例程(footprintsounds()); } IEnumerator声音() { 如果(接地) { 如果(x>0 | | z>0) { //试图颠倒顺序,但仍然没有结果 返回新的等待秒(1f); FindObjectOfType().Play(“Foot”); } } },c#,unity3d,C#,Unity3d,这是从我的脚本中提取的一段代码。 我曾希望,因为我正在使用一个协同程序,它会等待WaitForSeconds的间隔,然后再次播放,但它只是无限重复,好像没有计时器或任何东西 你能帮我弄清楚为什么这个不能运行吗? 关于如何编写足迹脚本的提示也将非常感谢 注:当我说它是一个片段时,我的意思是:这不是全部代码,更新函数只是为了让你知道我没有弄乱我引用的变量。还有另一种方法,那就是完全抛弃协同程序,只使用更新函数: void Update() { isGrounded = Physics.CheckSp
注:当我说它是一个片段时,我的意思是:这不是全部代码,更新函数只是为了让你知道我没有弄乱我引用的变量。还有另一种方法,那就是完全抛弃协同程序,只使用更新函数:
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
StartCoroutine(FootStepSounds());
}
IEnumerator FootStepSounds()
{
if (isGrounded)
{
if (x > 0 || z > 0)
{
//tried to reverse the order, still nothing
yield return new WaitForSeconds(1f);
FindObjectOfType<AudioManager>().Play("Foot");
}
}
}
如果你想的话,你可以更喜欢它,并且在两步之间的最短时间内暴露一个浮动变量。但是,这应该可以让您开始了。因为您每帧都调用它?我考虑过,但我认为协程WaitForSeconds可以解决这个问题,因为我告诉它等待。你能给我推荐一种解决方法吗?要以一定的间隔无限重复这个调用,你可以在协同程序中做一个while循环,比如while(enabled){waitForSecond()do footprint calculation;}并调用OnEnable,not on update你每一帧都要启动一个新的协同程序。。。
public AudioManager audioManager;
private float timer = 0f;
void Update ( )
{
isGrounded = Physics.CheckSphere ( groundCheck.position, groundDistance, groundMask );
x = Input.GetAxis ( "Horizontal" );
z = Input.GetAxis ( "Vertical" );
timer += Time.deltaTime;
if ( isGrounded && timer > 1f )
{
if ( x > 0 || z > 0 )
{
timer = 0;
audioSource.Play ( "Foot" );
}
}
}