Quantcast
Channel: Questions in topic: "quaternion.slerp"
Viewing all articles
Browse latest Browse all 133

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

$
0
0
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?

Viewing all articles
Browse latest Browse all 133

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>