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.
CODE:
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.
Ethan
Answer by dhore · Aug 26, 2016 at 05:56 AM
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].
I looked at it and i think it will REALLY help me with my project but i have one more question: Is it possible to add nodes to a path i create through a script? if so, how?
Tx for the quick respond. Ethan
You mean something like this?
int goingToPosIndex = 0;
public Vector3[] path;
Vector3 myStartPos;
public float speed;
void Start()
{
myStartPos = transform.position;
}
void Update()
{
if (goingToPosIndex < path.length)
{
if (transform.position != path[goingToPosIndex])
{
Vector3 startPos = Vector3.zero;
if (goingToPosIndex == 0)
startPos = myStartPos;
else
startPos = path[goingToPosIndex - 1];
transform.position = Vector3.Lerp(startPos, path[goingToPosIndex], Time.time * speed);
}
else
goingToPosIndex++;
}
}
@dhore I think i didn't give you all the information, my bad. I have a game object that records every period of time the location of the player and after the player press a button there is a cube that goes at the recorded "trail" of points. I was also looking at iTweens visual path and i think that it would be very helpful to be able to visualize the path the cube will go as a path.
So what i'm asking is, is it possible to "record" the location of the player as points in a path?