- Home /
Easing rotation based on angles, not speed
I am trying to make a simple speedometer / revolution counter, in which the needle eases out at certain positions after acceleration.
I can get it to accelerate nicely by just adding Time.deltaTime to itself like this:
IEnumerator StartAcceleration(Positions toPosition)
{
float currentRotation = needleHub.transform.rotation.z;
float originalRotation = needleHub.transform.rotation.z;
float toRotation = resultRotations[toPosition].z - 0.075f
float velocity = 0;
while (currentRotation > toRotation)
{
velocity += Time.deltaTime * 10f;
// Not working
//needleHub.transform.Rotate (0, 0, Mathf.SmoothStep(originalRotation, toRotation, velocity));
needleHub.transform.Rotate (0, 0, velocity);
currentRotation = needleHub.transform.rotation.z;
yield return new WaitForEndOfFrame();
}
I think this is right. It looks right anyway.
The only problem is that all examples on easing rotation I find are based on time, from 0 to 1, where smoothstep, slerps and dampangles are based on this.
But I cannot use time, since I don't know if the needle has to go from 0mph to 80mph or 0mph to 40mph. If I used time, the speed of the needle would not be the same.
So how do I smooth based on the angle in resultRotations[toPosition]?
Regards.
Answer by robertbu · Feb 13, 2013 at 04:02 PM
I'm a bit confused about what is driving what here. But when I do these things, I first define something that goes from 0 to 1. It might be the current velocity divided by the maximum velocity, it might be a timer with current time over total time. To get it to ease, I use an easing function.
With an easing function you map this 0 to 1 value through a curve. Sine between 0 and PI/2 works well for a lot of easing. X^Y is my second most popular curve for my easing. So if you were using a timer, velocity becomes (untested):
velocity = Mathf.Sin(Mathf.PI / 2.0f * curTimer/totalTime) * maxVelocity;
I am not sure how to map currentRotation to 0 and toRotation to 1. They represent to angles right now. $$anonymous$$g. they are respectively 0.71 and -0.73 in what I am working on right now.
$$anonymous$$y problem is that usually in the examples they make a "timer" map from 0 to 1, but I cant rely on time, since it takes different amount of time for my needle to go from 0 to 1 as it does to go from, say, 0 to 3. Does it make sense?
For easing, you can drive your motion three ways: speed, time, or a third I'll call friction. Note that you can use time to drive a "speed" motion just by setting the time based on distance. Your number below either represent really small angles, fractions of rotations, or radians. I'm not sure which, but your travel distance will always be your end position/rotation - start position/rotation. So for your given numbers above.
fromRotation = 0.71;
toRotation = -.73;
totalRotation = toRotation - fromRotation; // -1.44
So lets say you want your speed to be -0.4 units/per second. You can implement that in two ways. You could calculate the total time by totalRotation / -0.4 which would equal 3.6, so you would set your timer for 3.6 seconds for this rotation.
As an alternate, you could have a variable called currRotation which starts equal to the toRotation and then each frame and add -0.4 * Time.deltaTime to it each frame. The fraction is then becomes:
fraction = (currRotation - toRotation) / totatRotation;
To get the actual rotation used you would do something like:
actualAngle = fromRotation + totalRotation * $$anonymous$$athf.Sin($$anonymous$$athf.PI / 2.0f * fraction);
Thank you for this. I think I get you.
So what I want is for the needle to go from 0 ($$anonymous$$) to 3 (max), and that takes 3 seconds. Then if I only do an animation where the needle goes from 0 go 1, it should take 1/3 of the time. Given this example i realize it would be easy just to type seconds as x/3, but I was wondering if there was a more generic formula for this kind of arithmetic.
Given the speed should not be 1/3 and there was more than 3 "steps" for instance.
Typically when dealing with speed issues, I think about units/second. So if I have a rotation in degrees, I decide how many degrees per second I want it to move. Then in the code I multiply the degreesPerSecond variable by Time.deltaTime to calculate the movement per frame.
Your answer
Follow this Question
Related Questions
How can I fix this randomly rotating gun to an unknown degrees? 1 Answer
Camera Zoom from rotation. 0 Answers
Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion? 1 Answer
Curve player movement 1 Answer
How do i determin if an objects rotation is between 2 values on each axis? 0 Answers