以速度1旋转对象0到90然后等待90到0度,1速度统一c#

时间:2017-06-19 18:44:16

标签: c# unity3d unityscript

我希望以1档速度将对象旋转0到90度 - >然后等待3秒--->然后以1速度再次旋转90度至0度 - >然后等待3秒

我需要循环中的上述过程

我成功了,但它在Start Function

中只运行了一次

在更新功能中,它无法正常工作

在我的启动功能代码

下面
IEnumerator Start()
{
    StartCoroutine( RotateMe1(Vector3.forward * 90f, 1f));
    yield return new WaitForSeconds(3);
    StartCoroutine(RotateMe2(Vector3.forward * 0f, 1f));
    yield return new WaitForSeconds(3);
}

IEnumerator RotateMe1(Vector3 byAngles1, float inTime1)
{
    var fromAngle1 = transform.rotation;
    var toAngle1 = Quaternion.Euler(transform.eulerAngles + byAngles1);
    for (var t = 0f; t < 1; t += Time.deltaTime / inTime1)
    {
        transform.rotation = Quaternion.Lerp(fromAngle1, toAngle1, t);
        yield return null;
    }
}

IEnumerator RotateMe2(Vector3 byAngles2, float inTime2)
{
    var fromAngle2 = transform.rotation;
    var toAngle2 = Quaternion.Euler(transform.eulerAngles + byAngles2);
    for (var t = 0f; t < 1; t += Time.deltaTime / inTime2)
    {
        transform.rotation = Quaternion.Lerp(fromAngle2, toAngle2, t);
        yield return null;
    }
}

在我的更新功能代码

下面
void Update()
{
    StartCoroutine(RotateMe1(Vector3.forward * 90f, 1f));
    StartCoroutine(Wait());
    StartCoroutine(RotateMe2(Vector3.forward * 0f, 1f));
    StartCoroutine(Wait());
}

IEnumerator RotateMe1(Vector3 byAngles1, float inTime1)
{
    var fromAngle1 = transform.rotation;
    var toAngle1 = Quaternion.Euler(transform.eulerAngles + byAngles1);
    for (var t = 0f; t < 1; t += Time.deltaTime / inTime1)
    {
        transform.rotation = Quaternion.Lerp(fromAngle1, toAngle1, t);
        yield return null;
    }
}

IEnumerator RotateMe2(Vector3 byAngles2, float inTime2)
{
    var fromAngle2 = transform.rotation;
    var toAngle2 = Quaternion.Euler(transform.eulerAngles + byAngles2);
    for (var t = 0f; t < 1; t += Time.deltaTime / inTime2)
    {
        transform.rotation = Quaternion.Lerp(fromAngle2, toAngle2, t);
        yield return null;
    }
}

IEnumerator Wait()
{
    yield return new WaitForSeconds(3);
}

Here i recorded Video for issue of Update Function

我将上面的脚本附加在上面附带视频的AX上

请帮帮我

2 个答案:

答案 0 :(得分:2)

发生的事情是你每帧都开始cooruines而不等待一个人完成。这导致数百甚至数千个协同程序试图同时修改对象旋转。抱歉,您无法在更新功能中等待。您可以尝试使用boolean变量,但使用协同程序来完成此类操作。

如果您希望它永远旋转,请将其置于while循环中。

void Start()
{
    StartCoroutine(rotateForever());
}

IEnumerator rotateForever()
{
    while (true)
    {
        StartCoroutine(RotateMe1(Vector3.forward * 90f, 1f));
        yield return new WaitForSeconds(3);
        StartCoroutine(RotateMe2(Vector3.forward * 0f, 1f));
        yield return new WaitForSeconds(3);
    }
}

您可以优化rotateForever功能:

IEnumerator rotateForever()
{
    WaitForSeconds waitTime = new WaitForSeconds(3);
    while (true)
    {
        StartCoroutine(RotateMe1(Vector3.forward * 90f, 1f));
        yield return waitTime;
        StartCoroutine(RotateMe2(Vector3.forward * 0f, 1f));
        yield return waitTime;
    }
}

答案 1 :(得分:2)

您可以使用单个协程来完成工作,而不是使用它。 基本上,这个。

void Start()
{
    StartCoroutine(RotateMe(Vector3.forward * 90f, 1f));
}

IEnumerator RotateMe(Vector3 byAngles1, float inTime1)
{
    while (true)
    {
        var fromAngle1 = transform.rotation;
        var toAngle1 = Quaternion.Euler(transform.eulerAngles + byAngles1);
        for (var t = 0f; t < 1; t += Time.deltaTime / inTime1)
        {
            transform.rotation = Quaternion.Lerp(fromAngle1, toAngle1, t);
            yield return null;
        }
        yield return new WaitForSeconds(3);
        byAngles1 *= -1;
    }
}