Rotate from current rotation to another over time problem. please help.

I want to make an object rotate to the saved direction of another object and it doesn’t work as expected.


using UnityEngine;
using System.Collections;
using System.Linq;

public class TimeReflectionRotations : MonoBehaviour {

	public Quaternion[] playerRotations;
	public float[] playerRotationsChangeTime;
	public float speed;
	public float time;
	public Vector3 pointToGo;
	public float positionTimeNumber;

	[SerializeField]private bool rotating;
	private float degPerSecY;

	void Start () {

	void Update () {
		if (this.transform.position != this.GetComponent<TimeReflectionPositions> ().pointToGo) {
			if (time <= playerRotationsChangeTime.Last ()) {
				if (playerRotationsChangeTime.Contains (time)) {
					float timeToCompleteRotation = Vector3.Distance (this.transform.position, this.GetComponent<TimeReflectionPositions> ().pointToGo) / speed;
					print (timeToCompleteRotation);
					degPerSecY = (this.transform.rotation.y - playerRotations [System.Array.IndexOf (playerRotationsChangeTime, positionTimeNumber)].y) / timeToCompleteRotation;
					print (degPerSecY);
					rotating = true;
		} else {
			rotating = false;
		if (rotating) {
			this.transform.Rotate (0, degPerSecY * Time.deltaTime, 0);
			print ("Rotate to: (0, " + degPerSecY * Time.deltaTime + ", 0)");

Please help me.
If you have any questions you can ask below and i’ll answer as fast as i can.


Your code seems very overly complicated…

You should be using something like Quaternion.Slerp.

This example will rotate 90 degrees to the right:

public float rotSpeed;
Quaternion startRot, endRot;

void Start()
	startRot = Quaternion.LookRotation(transform.forward);
	endRot = Quaternion.LookRotation(transform.right);

void Update()
	if (transform.rotation != endRot)
		transform.rotation = Quaternion.Slerp(startRot, endRot, Time.time * rotSpeed);

You can also use Vector3.Lerp for positions (I noticed a reference to a position script).

Another option (which would give you better results too) would be to use the iTween Plugin, which is free on the Asset Store [link here] and has some decent documentation as well [docs here].