how to rotate a gameobject back and forth smoothly ,How to rotate a gameObject back and forth smoothly with a specified time interval.

Hello. So I am have a gameobject that needs to rotate backwards smoothly wait for a few seconds and then rotate back to its original position again smoothly.Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LookBehind : MonoBehaviour

public float turnRate = 4f;
public float waitTime;

private float timeSinceLastTurned;

private Quaternion originalRotation;

private float rotationSpeed = 10.0f;

 // Start is called before the first frame update
void Start()
    originalRotation = transform.rotation;

// Update is called once per frame
void Update()
    timeSinceLastTurned += Time.deltaTime;

    if (timeSinceLastTurned >= turnRate) 
       transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, -180, 0), Time.deltaTime * rotationSpeed);
        waitTime += Time.deltaTime;
        if ( waitTime >= turnRate ) 
            transform.rotation = Quaternion.Slerp(transform.rotation,originalRotation, Time.deltaTime * rotationSpeed);
            waitTime = 0;
            timeSinceLastTurned = 0;



Now the problem is that the gameobject rotates smoothly backwards using Quaternion.Slerp but it shows irregular rotation when it needs to rotate back to its original position.I think the second Quaternion.Slerp is causing this problem. Could you help me find a solution to this problem by making changes in the same code?

slerp should not be used like that, you can change Slerp for RotateTowards, and should work, you need to use Slerp iun particular for any reason? since its a bit harder to explain, slerp interpolates between 2 positions, so for linear interpolating between 2 objects you should not used the transform.position as the first position.