【问题标题】:How to make this coroutine work like update function如何使这个协程像更新函数一样工作
【发布时间】:2020-01-02 20:31:01
【问题描述】:

基本上,此代码将游戏对象的大小从大到小,然后从小到大..(重复)

现在,如果您希望通过在游戏运行时将 bool 可重复设置为 true 或 false 来使其变小变大或变大变小,您会怎么做。

就像,在更新函数中,我们有 if 函数。我希望在鼠标按下时发生yield return RepeatLerp(minScale, maxScale, duration);,在鼠标启动时发生yield return RepeatLerp(tramsform.localscale // (current size of object), minScale, duration);

public class TouchMechanic : MonoBehaviour {
    Vector3 minScale;
    public Vector3 maxScale;
    public bool repeatable;
    public float speed = 2f;
    public float duration = 5f;  
    public float timeStartedlerping ;  

    public float z;

IEnumerator Start()
     {
         minScale = transform.localScale;
         while (repeatable)
         {
             yield return RepeatLerp(minScale, maxScale, duration);
             yield return RepeatLerp(maxScale, minScale, duration);
         }
     }
     public IEnumerator RepeatLerp(Vector3 a, Vector3 b, float time)
     {
         float i = 0.0f;
         float rate = (1.0f / time) * speed;


             while (i < 1.0f)
             {
                 i += Time.deltaTime * rate;
                 transform.localScale = Vector3.Lerp(a, b, i);
                 yield return null;
             }    

     }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您似乎希望您的代码像这样工作:

    1. 协程被调用时,当前刻度记录为最小刻度。
    2. 对象的缩放比例达到 maxScale。
    3. 对象的比例恢复为原始比例。

    您的代码有点过于复杂。希望这段代码能达到你想要的效果,但可以稍微简化一下。

    IEnumerator ExpandContract(Vector3 minScale, Vector3 maxScale, float duration)
    {
        float time = 0f;
        while(transform.localScale < maxScale) //While the current scale has not reached its max, keep expanding.
        {
            transform.localScale = Vector3.Lerp(minScale, maxScale, time / duration);
            time += time.deltaTime;
            yield return null;
        }
        time = 0f;
        while(transform.localScale > minScale) //While the current scale has not yet returned to its original size, keep shrinking.
        {
            transform.localScale = Vector3.Lerp(maxScale, minScale, time / duration);
            time += time.deltaTime;
            yield return null;
        }
    }
    

    为了调用这个协程,你需要调用:

    StartCoroutine(ExpandContract(minScale, maxScale, duration))
    

    由于该方法未标记为公共,您只能从同一方法中启动协程。您可以通过类似这样的方式公开它

    public void StartExpandContract()
    {
        Vector3 minScale = transform.localScale;   
        StartCoroutine(ExpandContract(minScale, maxScale, duration));
    }
    

    然后,您可以从项目中的任何其他位置调用“StartExpandContract()”方法,只要您有对 TouchMechanic 组件的引用。或者,您可以添加一个 Update() 循环来检查用户输入并在按下鼠标或按键时调用“StartExpandContract()”,如下所示:

    void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            StartExpandContract();
        }
    }
    

    整个脚本如下所示:

    public class TouchMechanic : MonoBehaviour 
    {
        public Vector3 maxScale;
        public float duration = 5f;  
        void Update()
        {
            if (Input.GetKeyDown("space"))
            {
                StartExpandContract();
            }
        }
    
        public void StartExpandContract()
        {
            Vector3 minScale = transform.localScale;   
            StartCoroutine(ExpandContract(minScale, maxScale, duration));
        }
    
        IEnumerator ExpandContract(Vector3 minScale, Vector3 maxScale, float duration)
        {
            float time = 0f;
            while(transform.localScale < maxScale) //While the current scale has not reached its max, keep expanding.
            {
                transform.localScale = Vector3.Lerp(minScale, maxScale, time / duration);
                time += time.deltaTime;
                yield return null;
            }
            time = 0f;
            while(transform.localScale > minScale) //While the current scale has not yet returned to its original size, keep shrinking.
            {
                transform.localScale = Vector3.Lerp(maxScale, minScale, time / duration);
                time += time.deltaTime;
                yield return null;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-01
      • 2023-04-06
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 2012-07-20
      • 2016-01-11
      • 1970-01-01
      相关资源
      最近更新 更多