C# IEnumerator While循环:Mathf.Lerp,游戏继续运行

C# IEnumerator While循环:Mathf.Lerp,游戏继续运行,c#,unity3d,C#,Unity3d,我试图在2秒钟内从一组2个值平滑地进行lerp,然后在2秒钟内进行反向操作。我一直在尝试使用Mathf.Lerp或Smoothstep,但似乎值只更改了一次,然后就没有到达IEnumerator方法的其余部分。我的问题是IEnumerator实现还是在Lerp中 void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.CompareTag("Player")) { Star

我试图在2秒钟内从一组2个值平滑地进行lerp,然后在2秒钟内进行反向操作。我一直在尝试使用Mathf.Lerp或Smoothstep,但似乎值只更改了一次,然后就没有到达IEnumerator方法的其余部分。我的问题是IEnumerator实现还是在Lerp中

void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Player")) {
            StartCoroutine(DistortScreenFX());
            puh.gm.score += 500;
            SoundManager.PlaySound("lightspeed1");
            Destroy(gameObject);
        }

        if (other.gameObject.CompareTag("Asteroid")) {
            Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
        }

        if (other.gameObject.CompareTag("Ability")) {
            Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
        }
    }


        IEnumerator DistortScreenFX() {

        ChromaticAberration abbFX; //ramp then reset to 0
        LensDistortion lensFX; //ramp then reset to 0

        pp.profile.TryGetSettings(out abbFX);
        pp.profile.TryGetSettings(out lensFX);

        var startTime = Time.realtimeSinceStartup;
        float duration = 2.0f;
        float t = 0;
        while (Time.realtimeSinceStartup < startTime + duration) {
            t = (Time.realtimeSinceStartup - startTime) / duration;
            abbFX.intensity.value = Mathf.SmoothStep(0.0f, 1.0f, t);
            lensFX.intensity.value = Mathf.SmoothStep(-25.0f, -100.0f, t);

            yield return null;
        }

        startTime = Time.realtimeSinceStartup;
        while (Time.realtimeSinceStartup < startTime + duration) {
            t = (Time.realtimeSinceStartup - startTime) / duration;
            abbFX.intensity.value = Mathf.SmoothStep(1.0f, 0.0f, t);
            lensFX.intensity.value = Mathf.SmoothStep(-100.0f, -25.0f, t);

            yield return null;
        }

    }
无效OnCollisionInter2D(碰撞2D其他)
{
if(other.gameObject.CompareTag(“玩家”)){
start例程(screenFX());
puh.gm.得分+=500;
SoundManager.PlaySound(“lightspeed1”);
摧毁(游戏对象);
}
if(other.gameObject.CompareTag(“小行星”)){
Physics2D.IgnoreCollision(other.gameObject.GetComponent(),GetComponent());
}
if(other.gameObject.CompareTag(“能力”)){
Physics2D.IgnoreCollision(other.gameObject.GetComponent(),GetComponent());
}
}
IEnumerator屏幕Fx(){
色差abbFX;//然后将斜率重置为0
lensDistoration lensFX;//然后将斜坡重置为0
pp.profile.TryGetSettings(out abbFX);
pp.profile.TryGetSettings(out lensFX);
var startTime=Time.realtimesincestart;
浮动持续时间=2.0f;
浮动t=0;
while(Time.realtimesincestart
如果您使用start例程(扭曲屏幕Fx())运行此函数,则该函数很可能引发异常,导致其终止。检查日志,看看是否有错误


如果您没有使用Start例程调用它,那么您调用它是错误的。

如果您使用Start例程(扭曲屏幕Fx())运行此函数,那么该函数很可能引发异常,导致其终止。检查日志,看看是否有错误


如果您没有使用startcroutine调用它,那么您调用它是错误的。

您正在销毁运行协同程序的游戏对象。
放置
Destroy(游戏对象)在协同程序结束时(在最后一次
返回null之后)。

您正在销毁运行协同程序的游戏对象。
放置
Destroy(游戏对象)
在协同程序结束时(在最后一次
返回null之后)。

您如何调用此方法?运行此协同程序的MonoBehavior在执行期间是否处于活动状态?您确定返回类型应为IEnumerator吗?@mcrvaz我正在OnCollisionInter2D(Collision2D other)内部使用Start例程(扭曲屏幕Fx())您是否在碰撞时销毁游戏对象?你能发布全部代码吗?@mcrvaz我已经编辑了主要问题,添加了与问题有关的全部代码。你如何调用此方法?运行此协同程序的MonoBehavior在执行期间是否处于活动状态?您确定返回类型应为IEnumerator吗?@mcrvaz我正在OnCollisionInter2D(Collision2D other)内部使用Start例程(扭曲屏幕Fx())您是否在碰撞时销毁游戏对象?你能发布全部代码吗?@mcrvaz我已经编辑了主要问题,添加了与问题有关的全部代码。我在日志中没有看到错误,我正在使用StartRoutine我在日志中没有看到错误,我正在使用StartRoutine