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?
↧