-3
\$\begingroup\$

i want to Rotate my object 0 to 90 degree with 1 speed --> then wait for 3 Sec ---> Then Rotate again 90 to 0 degree with 1 speed --> Then wait for 3 sec

i need above process in loop

i m success with it but it working only 1 time in Start Function

In update function its not working

below my code

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;
    }
}

Below my Update Function code

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

i attached above script on AXE in attached above Video

Please help me

\$\endgroup\$
  • \$\begingroup\$ Please don't cross post on other stack exchange sites. Only post on the most appropriate site for your question. If you've posted it and feel like it was a mistake, remove it first, then post it at different site. \$\endgroup\$ – MichaelHouse Jun 21 '17 at 18:49
2
\$\begingroup\$

Use a Tweening engine like DOTween: http://dotween.demigiant.com/

Then, you create a script like TestRotation and achieve your rotation in 3 lines of code:

using UnityEngine;
using DG.Tweening;

public class TestRotation : MonoBehaviour {

    void Start(){
        Sequence sequence = DOTween.Sequence(); // create a sequence
        sequence.Append(this.transform.DORotate(new Vector3(90f, 0f, 0f), 1f).SetDelay(3f)); // rotate it by 90 on X axis for 1 second, and delay it for 3 seconds
        sequence.SetLoops(-1); // repeat continously
    }

}

Tweening makes the life 300% easier, really.

\$\endgroup\$
  • \$\begingroup\$ Tweening makes the life 300% easier, really. And so many people who respond "ew, plugins, I wanna do it myself for the EXPEREUNCE." \$\endgroup\$ – Draco18s Jun 21 '17 at 17:45
  • \$\begingroup\$ Getting into Unity without knowledge about OOP and particular languages on at least intermediate level is a waste of time ;> \$\endgroup\$ – Jacob Jun 22 '17 at 7:02
0
\$\begingroup\$

In RotateMe2 you have this line: var toAngle2 = Quaternion.Euler(transform.eulerAngles + byAngles2);

The current transform.eulerAngles is (90,0,0). The byAngles2 (passed to the function from Start) is (0,0,0).

(90,0,0) + (0,0,0) = (90,0,0).

Surprise, nothing changes when you attempt to rotate. You've confused byAngles with toAngles.

Also, RotateMe1 and RotateMe2 are identical, except with differently named paramter and local variables names. So there's really no reason for it at all.

Corrected code:

IEnumerator Start()
{
    StartCoroutine( RotateMe1(Vector3.forward * 90f, 1f));
    yield return new WaitForSeconds(3);
    StartCoroutine(RotateMe1(Vector3.forward * -90f, 1f));
    yield return new WaitForSeconds(3);
}
\$\endgroup\$
  • \$\begingroup\$ i already tried (Vector3.forward * 90f, 1f) & (Vector3.forward * -90f, 1f) but not working. \$\endgroup\$ – Alpesh Savaliya Jun 19 '17 at 19:54
0
\$\begingroup\$

You can use a single coroutine to do the job, use it instead. Basically, this.

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(1);
        byAngles1 *= -1;
    }
}
\$\endgroup\$
-1
\$\begingroup\$

i got Answer from @Programmer in Stackoverflow.

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

IEnumerator rotateForever()
{
    while (true)
    {
        StartCoroutine(RotateMe1(Vector3.forward * -90f, 1f));
        yield return new WaitForSeconds(3);
        StartCoroutine(RotateMe2(Vector3.forward * 90f, 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;
    }
}

just need to do in repeat forever.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.