C# 尝试使用协程使方法暂停,我做错了什么?

C# 尝试使用协程使方法暂停,我做错了什么?,c#,unity3d,coroutine,ienumerator,C#,Unity3d,Coroutine,Ienumerator,我在这里遇到了一个问题,我的代码似乎非常好。它运行,然后当一个外部脚本从任何一个spike方法中拉出来时,它的行为就好像协程根本不存在一样,从减速到全速。 除了协同程序不会导致等待之外,没有其他错误或问题。我是否遗漏了一些明显的内容?您不能将必须暂停的代码放入正常函数中。您必须将它放在协同程序函数中 例如,在Bigslow函数中,Speed变量将设置为0。暂停协同程序将启动。Unity将在暂停减速功能中仅等待一帧,然后返回大减速,其中速度设置为1。它不会等待,因为您正在从void函数启动它 您有

我在这里遇到了一个问题,我的代码似乎非常好。它运行,然后当一个外部脚本从任何一个spike方法中拉出来时,它的行为就好像协程根本不存在一样,从减速到全速。
除了协同程序不会导致等待之外,没有其他错误或问题。我是否遗漏了一些明显的内容?

您不能将必须暂停的代码放入正常函数中。您必须将它放在协同程序函数中

例如,在
Bigslow
函数中,
Speed
变量将设置为
0
暂停
协同程序将启动。Unity将在
暂停减速
功能中仅等待一帧,然后返回
大减速
,其中速度设置为
1
。它不会等待,因为您正在从
void
函数启动它

您有两个选择:

1。将
slow
Bigslow
函数改为
IEnumerator
void
,然后在调用
start例程
时生成

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 [RequireComponent(typeof(Rigidbody2D))]

 public class Movement : MonoBehaviour
 {
     //storage for the object's rigidbody
     public Rigidbody2D rb2D;
     //the public modifier for speed so i can edit from outside
     public float speed;
     //reliably take movements each second in an even, reliable manner. 
     void FixedUpdate()
     {
         //i move left and right if  i use "a" or "d or "left" or "right" 
         float moveX = Input.GetAxis("Horizontal");
         //I move up and down if i use "w" or "s" or "up" or "down"
         float moveY = Input.GetAxis("Vertical");
         //Vector inputs the move directions into a vector2 so i can move
         Vector2 move = new Vector2(moveX, moveY);
         rb2D.velocity = move * speed;
     }
     //following two methods are for the respective spikey builds
     public void slow()
     {
         print("Shit, spike's got me!");
         speed = .3f;
         StartCoroutine(PauseTheSlowdown(2.1f));
         speed = 1;
     }
     public void Bigslow()
     {
         print("HOLY CRAP THAT HURT");
         speed = 0f;
         StartCoroutine(PauseTheSlowdown(3.7f));
         speed = 1;
     }

     IEnumerator PauseTheSlowdown(float seconds)
     {
         print("Pause this crap");
         yield return new WaitForSecondsRealtime(seconds);
     }

 }
在调用
startcroutine
之前,请注意
yield return

2。将要执行的代码移动到
pausetheslown
corroutine函数并等待一段时间:

public IEnumerator slow()
{
    print("Shit, spike's got me!");
    speed = .3f;
    yield return StartCoroutine(PauseTheSlowdown(2.1f));
    speed = 1;
}

public IEnumerator Bigslow()
{
    print("HOLY CRAP THAT HURT");
    speed = 0f;
    yield return StartCoroutine(PauseTheSlowdown(3.7f));
    speed = 1;
}

IEnumerator PauseTheSlowdown(float seconds)
{
    print("Pause this crap");
    yield return new WaitForSecondsRealtime(seconds);
}

这回答了很多问题,谢谢。我马上就去试试。
public void slow()
{
    print("Shit, spike's got me!");
    StartCoroutine(PauseTheSlowdown(2.1f, .3f, 1));
}
public void Bigslow()
{
    print("HOLY CRAP THAT HURT");
    StartCoroutine(PauseTheSlowdown(3.7f, 0f, 1));
}

IEnumerator PauseTheSlowdown(float seconds, float before, float after)
{
    print("Pause this crap");
    speed = before;
    yield return new WaitForSecondsRealtime(seconds);
    speed = after;
}