Hello. This is my script for the rotation of my object. Currently it instantly goes to the random degree, how would I modify my script to make it animate the rotation? Thanks
function Start(){
InvokeRepeating("FR", 0,1);
}
function FR (){
transform.Rotate (Vector3.up, Random.Range(90.0, 270.0));
}
I looked at some of your recent questions on rotating, and I'm very confused as to what you want to do, each one is asking something that would not let you do another. With that being said, I'll take a shot answering your question.
It depends on how you want it to rotate: at a set speed or always reaching the goal. You could have a set rotation speed that will always be constant, but only might reach your target angle in a single second, or it might have to wait for most of the second because the angle was very close to its current angle.
/*------------------------------------------------------------
This will cause the object to rotate at a constant speed.
We call RandomRotate (FR was confusing for me, feel free to change it back).
FR chooses a random angle. Then we create a quaternion using that angle.
Then we rotate using Quaternion.RotateTowards() at a constant speed until repeatTime seconds have passed.
Then we pick a new angle and start rotating towards that.
------------------------------------------------------------*/
var repeatTime : float = 1.0;
var rotateSpeed : float = 45;
function Start () {
for(;;) {
RandomRotate();
yield WaitForSeconds(repeatTime);
}
}
function RandomRotate () {
var goal : float = Random.Range(90.0, 270.0);
var desiredAngle : Quaternion = Quaternion.AngleAxis(goal, Vector3.up);
var i : float = 0.0;
while(i < repeatTime) {
transform.rotation = Quaternion.RotateTowards(transform.rotation, desiredAngle, rotateSpeed * Time.deltaTime);
yield;
i += Time.deltaTime;
}
}
The other option would be interpolating (lerp and slerp) which would cause your object to rotate faster the further apart two angles are, but it will always arrive just in time.
/*------------------------------------------------------------
This will cause the object to rotate to a random point in the allotted amount of time.
Slerp spherically interpolates the rotation from one to the other based on the parameter i.
In this case, i is 0 at the beginning, and 1 after repeatTime seconds, meaning that it will start at one and finish at the other, give or take a small amount of error given Time.deltaTime won't ever be exactly repeatTime seconds.
Then we pick a new angle and start rotating towards that.
------------------------------------------------------------*/
var repeatTime : float = 1.0;
function Start () {
for(;;) {
RandomRotate();
yield WaitForSeconds(repeatTime);
}
}
function RandomRotate () {
var goal : float = Random.Range(90.0, 270.0);
var desiredAngle : Quaternion = Quaternion.AngleAxis(goal, Vector3.up);
var startRotation : Quaternion = transform.rotation;
//If we don't find a start point, we will get an easing effect at the end.
var i : float = 0.0;
while(i < repeatTime) {
transform.rotation = Quaternion.Slerp(startRotation, desiredAngle, i / repeatTime);
yield;
i += Time.deltaTime;
}
}