简体   繁体   English

Unity 3D:协程武器动画不会执行多次

[英]Unity 3D: Coroutine weapon animation won't execute more than once

I have a telescoping spike weapon for a game I'm working on that works perfectly fine once, but won't fire again afterwards:我有一个用于我正在开发的游戏的伸缩式尖刺武器,一次可以正常工作,但之后不会再次开火:

public void ExpandSequence()
{
    if (!mExpanding)
    {
        StartCoroutine(mExpand);
    }
        
}

private IEnumerator _ExpandSequence()
{
    mExpanding = true;
    mBaseGoal = transform.localPosition + mBaseDistance * Vector3.up;
    mConeGoal = mSpikeSections[5].localPosition + mConeDistance * Vector3.up;

    while (transform.localPosition.y > mBaseSpikeEnd || mSpikeSections[1].localPosition.y > mCylinderEnd
        || mSpikeSections[5].localPosition.y > mConeEnd)
    { //only using the first cylinder section in the condition since they all move the same distance
        
        transform.localPosition = Vector3.MoveTowards(transform.localPosition, mBaseGoal, mSpeed);

        mSpikeSections[1].localPosition = mSpikeSections[2].localPosition = mSpikeSections[3].localPosition = 
            mSpikeSections[4].localPosition = Vector3.MoveTowards(mSpikeSections[1].localPosition, Vector3.up * mCylinderDistance, mSpeed);

        mSpikeSections[5].localPosition = Vector3.MoveTowards(mSpikeSections[5].localPosition, mConeGoal, mSpeed);

        if (Mathf.Approximately(transform.localPosition.y, mBaseSpikeEnd) &&
            Mathf.Approximately(mSpikeSections[1].localPosition.y, mCylinderEnd) &&
            Mathf.Approximately(mSpikeSections[5].localPosition.y, mConeEnd))
        {
            mExpanding = false; //probably unnecessary
            transform.localPosition = mBaseGoal;
            mSpikeSections[5].localPosition = mConeGoal;

            yield break; //putting this here stops it from looping, but it still won't fire a second time
        }

        yield return null;
    }
    mExpanding = false;
}

public void CollapseSequence()
{
    if (!mCollapsing)
    {
        StartCoroutine(mCollapse);
    }
}

private IEnumerator _CollapseSequence()
{
    mCollapsing = true;
    mBaseGoal = transform.localPosition - mBaseDistance * Vector3.up;
    mConeGoal = mSpikeSections[5].localPosition - mConeDistance * Vector3.up;

    while (transform.localPosition.y < mBaseSpikeStart || mSpikeSections[1].localPosition.y < mCylinderStart
        || mSpikeSections[5].localPosition.y < mConeStart)
    { //only using the first cylinder section in the condition since they all move the same distance

        transform.localPosition = Vector3.MoveTowards(transform.localPosition, mBaseGoal, mSpeed);

        mSpikeSections[1].localPosition = mSpikeSections[2].localPosition = mSpikeSections[3].localPosition =
            mSpikeSections[4].localPosition = Vector3.MoveTowards(mSpikeSections[1].localPosition, Vector3.zero, mSpeed);

        mSpikeSections[5].localPosition = Vector3.MoveTowards(mSpikeSections[5].localPosition, mConeGoal, mSpeed);

        if (Mathf.Approximately(transform.localPosition.y, mBaseSpikeStart) && 
            Mathf.Approximately(mSpikeSections[1].localPosition.y, mCylinderStart) && 
            Mathf.Approximately(mSpikeSections[5].localPosition.y, mConeStart))
        {
            transform.localPosition = mBaseGoal;
            mSpikeSections[5].localPosition = mConeGoal;
            mCollapsing = false; //probably unnecessary
            yield break; //putting this here stops it from looping, but it still won't fire a second time
        }

        yield return null;
    }
    mCollapsing = false;
}

The if-statements I added at the end were for "damage control" but don't seem to change the general outcome.我在最后添加的 if 语句用于“损害控制”,但似乎不会改变总体结果。 The expand helper function "Expand()" seems to execute every time, but the actual functions as well as the collapse helper function only run once.扩展辅助函数“Expand()”似乎每次都执行,但实际函数以及折叠辅助函数只运行一次。 If anyone can help me figure this out it would be greatly appreciated.如果有人能帮我解决这个问题,我将不胜感激。

I don't see where you assign mExpand and mCollapse .我没有看到你在哪里分配mExpandmCollapse

But I guess you did it only once eg in Start .但我猜你只做过一次,例如在Start It then happens only once because the assigned routine is already finished after the first run.然后它只发生一次,因为分配的例程在第一次运行后已经完成。

It should probably rather be eg它可能应该是例如

public void ExpandSequence()
{
    if (!mExpanding)
    {
        mExpand = StartCoroutine(_ExpandSequence());
    }
}

and accordingly also for the collapse routine in order to be running a new IEnumerator / Coroutine instead of the one that already ran to the end.并且相应地也用于折叠例程,以便运行一个新的IEnumerator / Coroutine而不是已经运行到最后的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM