Thursday, May 12, 2016

c# - How to make something rotate a certain amount over an exact amount of time


I'm trying to make a plane rotate down a certain amount and 180 degrees horizontally over 2 seconds.


Basically I want it to take the plane 1 second to rotate to a horizontal-facing angle, then take the other second to rotate 180 degrees around to face the other direction.


The formula I'm currently using is:


    //If you're rotating, lower the speed and rotate the plane.
if (rotate == 1 && rotationPhase > 0) {
//If in phase 0, figure out which phase you need too go to.
if (angle > 90 && rotationPhase == 1) {

//Rotation phase 2 = rotating down
rotationPhase = 2;
rotationAngleTime = angle - 90;
}
else if (rotationPhase == 1) {
//Rotation phase 3 = rotating up
rotationPhase = 3;
rotationAngleTime = 90 - angle;
}


//After phase 0, if in phase 1 begin rotating down, if in phase 2 begin rotating up.
if (angle > 90 && rotationPhase == 2) {
transform.Rotate(0, 0, -rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2)));
angle -= rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2));
if (angle > 89 && angle < 91) {
angle = 90;
rotationPhase = 4;
rotationAngleTime = 180;
}
}

else if (rotationPhase == 3) {
transform.Rotate(0, 0, rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2)));
angle += rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2));
if (angle > 89 && angle < 91) {
angle = 90;
rotationPhase = 4;
rotationAngleTime = 180;
}
}


//After you have rotated to horizontal in phase 1/2, rotate around to face the other direction.
if (rotationPhase == 4) {
transform.Rotate(rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2)), 0, 0);
rotationAngle += rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2));
if (rotationAngle > 179) {
rotationAngle = 0;
rotationPhase = 0;
rotationAngleTime = 0;
}
}

}

I've tried many different things, but I can't figure out what to do.


EDIT: This seems to be a problem with Time.deltaTime. I can get the plane to rotate correctly, but for some reason it's not rotating over the right amount of time. I have a timer being subtracted by Time.deltaTime, and this animation happening, and the animation always finishes about half a second before the timer runs out.



Answer



EDIT: This makes the animation run properly, but it's still not running at the correct amount of time.


I figured out how to fix the problem myself, the solution was:


//If you're rotating, lower the speed and rotate the plane.
if (rotate == 1 && rotationPhase > 0) {
//If in phase 0, figure out which phase you need too go to.

if (angle > 90 && rotationPhase == 1) {
//Rotation phase 2 = rotating down
rotationPhase = 2;
rotationAngleTime = angle - 90;
}
else if (rotationPhase == 1) {
//Rotation phase 3 = rotating up
rotationPhase = 3;
rotationAngleTime = 90 - angle;
}


//After phase 0, if in phase 1 begin rotating down, if in phase 2 begin rotating up.
if (angle > 90 && rotationPhase == 2) {
transform.Rotate(0, 0, -rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2)));
angle -= rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2));
if (angle > 89 && angle < 91) {
angle = 90;
rotationPhase = 4;
rotationAngleTime = 180;
}

}
else if (rotationPhase == 3) {
transform.Rotate(0, 0, rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2)));
angle += rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2));
if (angle > 89 && angle < 91) {
angle = 90;
rotationPhase = 4;
rotationAngleTime = 180;
}
}


//After you have rotated to horizontal in phase 1/2, rotate around to face the other direction.
if (rotationPhase == 4) {
transform.Rotate(rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2)), 0, 0);
rotationAngle += rotationAngleTime * (Time.deltaTime * (rotationSpeed / 2));
if (rotationAngle > 179) {
rotationAngle = 0;
rotationPhase = 0;
rotationAngleTime = 0;
}

}
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...