-1
\$\begingroup\$

Hi I am new to unity so I am not super familiar with methods. I am trying to make my enemy rotate left then right double the amount that it rotated and back to the middle like this: +10 degree, -20 degree then +10 degree back to 0. I am trying transform.rotate however I believe I have a bad understanding of it and doing something wrong.

Edit: Even though I got the answer and it works the problem was that it inconsistently rotated like it went: +5 degree then -2 then +5 again. Next time I ran it it would be different values even though they were the same in the script.

Here is the method:

IEnumerator LookAroundArea(GameObject self)
    {
        if (10 >= Random.Range(1, 500))
        {
            Debug.Log("it runs");
            
            self.transform.Rotate(new Vector3(0, 1, 0), 45 * Time.deltaTime * 45);
            yield return new WaitForSeconds(1.0f);
            self.transform.Rotate(new Vector3(0, -1, 0), 45 * Time.deltaTime * 90);            
            yield return new WaitForSeconds(2f);
            self.transform.Rotate(new Vector3(0, 1, 0), 45 * Time.deltaTime * 45);
            yield return new WaitForSeconds(1.0f);
        }
        dontrunagain = true;
    }
```
\$\endgroup\$
1
  • \$\begingroup\$ What is the issue with this code? What behaviour does it give you, compared to what you want it to give you? \$\endgroup\$
    – Vaillancourt
    Nov 25 '21 at 19:52
1
\$\begingroup\$

Transform.Rotate rotates the object the full amount you asked for, instantly. It is not a tweening function that starts a gradual rotation that will complete over time.

If you want to rotate something over time, you can make your own coroutine for that:

IEnumerator Rotate(Transform self, Quaternion from, Quaternion to, float duration) {

    for (float t = 0; t < 1f; t += Time.deltaTime / duration) {
        // Rotate to match our current progress between from and to.
        self.rotation = Quaternion.Slerp(from, to, t);
        // Wait one frame before looping again.
        yield return null;
    }

    // Ensure we finish exactly at the destination orientation.
    self.rotation = to;
}

Then you can just invoke that coroutine and wait for it to finish its work:

IEnumerator LookAroundArea(Transform self)
{
    if (10 >= Random.Range(1, 500))
    {
        Debug.Log("it runs");
        
        // Cache the start, left, and right extremes of our rotation.
        Quaternion start = self.rotation;
        Quaternion left = start * Quaternion.Euler(0, -45, 0);
        Quaternion right = start * Quaternion.Euler(0, 45, 0);

        // Yield control to the Rotate coroutine to execute
        // each turn in sequence, and resume here after each
        // invocation of Rotate finishes its work.

        yield return Rotate(self, start, left, 1.0f);

        yield return Rotate(self, left, right, 2.0f);

        yield return Rotate(self, right, start, 1.0f);
    }
    dontrunagain = true;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .