Rotate Object 180 degrees wait a few seconds then rotate 180 degrees again

Hey Unity Comm, Can anyone of you help me with a problem.

I am making a platformer game and I want an object to rotate 180* wait about 3 seconds then rotate 180* can somebody help?

This is a line of code I made that rotates the player constantly:

public class RotateEnemy : MonoBehaviour
{
    void Update ()
    {
        transform.Rotate( 0, 1, 0 * Time.deltaTime );
    }
}

Hey HangusKhan115,
quick solution:

public class RotateObjectScript : MonoBehaviour
{
    // Timer
    private float currentTimer = 0f;

    // You can declare a value the timer will reset to
    private float maxTimer = 3f;

    private void Start()
    {
        currentTimer = maxTimer;
    }

    void FixedUpdate()
    {
        // Count down
        currentTimer -= Time.fixedDeltaTime;
        
        // When timer reaches zero
        if (currentTimer <= 0f)
        {
            // Rotate
            RotateObject();

            // Reset timer
            currentTimer += maxTimer;

            // OR you could even randomize the time to wait
            currentTimer += Random.Range(2.5f, 3.5f);
        } 
    }

    private void RotateObject()
    {
        // Rotate the object
        transform.Rotate(new Vector3(0, 180, 0));
    }
}

I hope this helps!

The code varies depending on if you want it to rotate your gameObject in the same direction or back. This is for the same direction:

    [Header("Tilt Settings")]
    [SerializeField] private float tiltsPerSecond = 1f;
    [SerializeField] private float extraTiltSpeed = 50f;
    [SerializeField] private float maxSnapTilt = 179f;
    [SerializeField] private float maxBreakSnapTilt = 359f;
    [SerializeField] private Vector3 maximumTilt = new Vector3 (0f, 180f, 0f);
    [SerializeField] private float secondsToWait = 3f;

    private Vector3 noTilt = new Vector3(0f, 0f, 0f);
    private bool doRotate;
    private bool snapped;
    private bool pauseRotate;
    private Vector3 currentEulerAngles;

    void Update()
    {

        if (doRotate)
        {
            StartCoroutine(nameof(RotateObject));
        }

    }

    private IEnumerator RotateObject()
    {   

        if (!pauseRotate && (Mathf.Ceil(transform.eulerAngles.y) == maxBreakSnapTilt))
        {
            transform.eulerAngles = noTilt;
            currentEulerAngles = transform.eulerAngles;
            doRotate = false;
            yield break;
        }

        else if (Mathf.Floor(transform.eulerAngles.y) == maxSnapTilt && !snapped)
        {
            snapped = true;
            transform.eulerAngles = maximumTilt;
            currentEulerAngles = transform.eulerAngles;
            pauseRotate = true;
            yield return new WaitForSeconds(secondsToWait);
            pauseRotate = false;
            snapped = false;
        }

        else if (!pauseRotate)
        {
            currentEulerAngles += new Vector3(0f, tiltsPerSecond, 0f) * Time.deltaTime * extraTiltSpeed;
            transform.eulerAngles = currentEulerAngles;
        }

    }

}

Also: I know that the code is unclean (especially the if statement conditions statements; any help would be appreciated) but I didnt find another solution. This should do the trick on slow rotations, Idk if it does on very fast rotations.

(If you want to change the axis to rotate on, you have to change some variables)

Is this @andrew-lukasik approved? :thinking:

Otherwise, I would highly recommend animating your GameObject and trying it that way, because the code for it is really complicated afaic

Try this code:

using System.Collections;
using UnityEngine;

public class RotateObjectScript : MonoBehaviour
{
    [Header("Timers")]
    [SerializeField] private float waitTime = 3f;

    [Header("Rotation")]
    [SerializeField] private Vector3 rotationToChange = new Vector3(0f, 180f, 0f);

    private bool doRotation;

    void FixedUpdate()
    {
        if (doRotation)
            RotateAndWait();       
    }

    private void RotateAndWait()
    {
        doRotation = false;
        transform.Rotate(rotationToChange);
        Invoke(nameof(FinishRotation), waitTime);
    }

    private void FinishRotation()
    {
        transform.Rotate(rotationToChange);
    }
}

This should work. Either, you do the this for a fast/instant snap rotation or you use the other one I posted for a slow rotation. These should be the solutions for your problem, please let us now if you have any further problems and thanks to @Tidali and @andrew-lukasik for the tips!

Thanks this one worked although it does snap its still appropriate for my game
my condolences

Put the rotate code in a for loop like this:

for (int i = 0; i > 180; i++) {
    transform.Rotate(0, 1, 0 * Time.deltaTime);
}

Put the following line under the for loop:

StartCoroutine(RotateBack(3));

And put this below Update:

IEnumerator RotateBack(int delay) {
yield return new WaitForSeconds(delay);
for (int i = 0; i > 180; i++) {
    transform.Rotate(0, 1, 0 * Time.deltaTime);
}
}

If I read the question exactly, this would be the easiest method:

public class RotateObjectScript : MonoBehaviour
{
    // Timer
    private float currentTimer = 3f;

    // Do rotate?
    private bool doRotate = true;

    private void Start()
    {
        RotateObject();
    }

    void FixedUpdate()
    {
        // Count down timer while above 0
        if (currentTimer > 0f)
        {
            // Count down
            currentTimer -= Time.fixedDeltaTime;
        }
        else if (doRotate)
        {
            // Rotate and
            RotateObject();

            // Disable further rotation
            doRotate = false;
        } 
    }


    private void RotateObject()
    {
        // Rotate the object
        transform.Rotate(new Vector3(0, 180, 0));
    }
}

This will rotate once by 180 degrees, wait 3 seconds, and then rotate again by 180 degrees.