In Vector math, what you’re talking about is called Interpolation. You have a few options when it comes to smoothly Interpolating between two numbers.
You can use Mathf.Lerp to Linearly Interpolate between two floats:
public float TimeSinceRotationStart = 0f;
public float RotationTime = 1f; // Take 1 second to rotate fully
public float CurrentLerpValue = 0f;
public float Start = 0f;
public float Finish = 100f;
void Update() {
float percentageDone = TimeSinceRotationStart / RotationTime; // The percentage value, represented as a number between 0 and 1
CurrentLerpValue = Mathf.Lerp(Start, Finish, percentageDone);
}
You can use Vector3.Lerp to Linearly Interpolate between two Euler Angles:
public float TimeSinceRotationStart = 0f;
public float RotationTime = 1f; // Take 1 second to rotate fully
public Vector3 CurrentLerpValue;
public Vector3 Start;
public Vector3 Finish = new Vector3(0f, 100f, 0f);
void Update() {
float percentageDone = TimeSinceRotationStart / RotationTime; // The percentage value, represented as a number between 0 and 1
CurrentLerpValue = Vector3.Lerp(Start, Finish, percentageDone);
}
You can use Quaternion.Slerp for Spherically Linearly Interpolated rotation. Slerp uses a Quaternion to create a smooth movement that smoothly accelerates then smoothly decelerates. You should give it a try and see if you like it, after trying out the other Lerp options of course.
public float TimeSinceRotationStart = 0f;
public float RotationTime = 1f; // Take 1 second to rotate fully
public Quaternion CurrentLerpValue;
public Quaternion Start = Quaternion.Euler(0f, 0f, 0f); // Convert 3D euler angles (x,y,z) to a 4D Quaternion (w,x,y,z)
public Quaternion Finish = Quaternion.Euler(0f, 100f, 0f);
void Update() {
float percentageDone = TimeSinceRotationStart / RotationTime; // The percentage value, represented as a number between 0 and 1
CurrentLerpValue = Vector3.Lerp(Start, Finish, percentageDone);
}
If you want to create a sort of Ping Pong type back and forth movement, implementing that with any of these functions is simple enough, but you can also use the PingPong (click here) method.