- Home /
Quadratic easing motion confusion!
Howdy!
So, for fun, I thought I would attempt to hand-code some easing functions into Unity, to move some cubes around. I had a look at Penner's tweening chapter (pdf here) for help with the equations. I've been able to get the easing in working, and the easing out working, but when I try to get an object to ease in and out and reach a specified position, I'm running into various problems. I have all my code here -- if you slap the code below on a gameobject and then watch it move, the problem should be clear.
I'm attempting to code the in-out easing motion by performing two half-tweens, which take place in half the time and go half the distance of the original tween. Then, when viewed together, it looks like a single in-out tween. I just can't figure out what's wrong with my equations. Any hints or solutions as to what it might be? Have you guys attempted this before? Do you know of any better explanations that are better than Penner's and (more importantly) mine? Thanks for your help!
enum Ease_Type { Algebraic, Quadratic, Cubic, Quartic, Quintic, Sinusoidal, Exponential, Circular, Customized} enum When_To_Ease { In, Out, Both } var Easing : Ease_Type; var When_Easing : When_To_Ease;
var x_destination = 1.0000; var y_destination = 1.0000; var duration_of_movement = 1.00000; var custom_degree = 0.00;
private var time_remaining; private var time_counter; private var x_coordinate; private var y_coordinate; private var x_starting_position; private var y_starting_position; private var started = false; private var start = false; private var invoked_start = false; private var half_duration_of_movement; private var adjusted_for_both = false; private var reset_time_for_second_half = false;
function Awake () { time_remaining = duration_of_movement; x_coordinate = transform.position.x; y_coordinate = transform.position.z; x_starting_position = transform.position.x; y_starting_position = transform.position.y; time_counter = 0.000; }
function Update () { if(!invoked_start) { Invoke("Startup", 1); invoked_start = true; }
if(start)
{
transform.position = Vector3(x_coordinate, y_coordinate, transform.position.z);
time_counter += Time.deltaTime;
if(time_counter < duration_of_movement)
{
switch (Easing)
{
case Easing.Algebraic:
Linear_Ease(duration_of_movement, x_starting_position, y_starting_position, x_destination, y_destination, time_counter);
break;
case Easing.Quadratic:
Quadratic_Ease(2, duration_of_movement, x_starting_position, y_starting_position, x_destination, y_destination, time_counter);
break;
}
}
}
}
function Startup () { start = true; }
//provides a linear movement. function Linear_Ease (duration_of_movement, x_starting_position, y_starting_position, x_change, y_change, time_since_called) { x_coordinate = x_change (time_since_called / duration_of_movement) + x_starting_position; y_coordinate = y_change (time_since_called / duration_of_movement) + y_starting_position; }
//provides quadratic and exponentially based movement. requires one more variable than the linear ease, the easing_degree variable. function Quadratic_Ease (easing_degree, duration_of_movement, x_starting_position, y_starting_position, x_change, y_change, time_since_called) { time_since_called = time_since_called / duration_of_movement; switch(When_Easing) { case When_Easing.In: x_coordinate = x_change Mathf.Pow(time_since_called, easing_degree) + x_starting_position; y_coordinate = y_change Mathf.Pow(time_since_called, easing_degree) + y_starting_position; break;
case When_Easing.Out:
x_coordinate = -1 * x_change * Mathf.Pow(time_since_called, easing_degree) * (time_since_called - 2) + x_starting_position;
y_coordinate = -1 * y_change * Mathf.Pow(time_since_called, easing_degree) * (time_since_called - 2) + y_starting_position;
break;
case When_Easing.Both:
if(time_counter/duration_of_movement <= .5)
{
x_coordinate = x_change/2 * Mathf.Pow(time_since_called, easing_degree) + x_starting_position;
y_coordinate = y_change/2 * Mathf.Pow(time_since_called, easing_degree) + y_starting_position;
}
if(time_counter/duration_of_movement > .5)
{
x_coordinate = -1 * x_change/2 * Mathf.Pow(time_since_called/2, easing_degree) * (time_since_called/2 - 2) + x_starting_position;
y_coordinate = -1 * y_change/2 * Mathf.Pow(time_since_called/2, easing_degree) * (time_since_called/2 - 2) + y_starting_position;
}
break;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612184052im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What is the best way to ease a rotation? (js) 1 Answer
Add Easing to Transform.Rotate 1 Answer
Apply multiple Lerps to the game value 1 Answer
iTween - Path inside other path 0 Answers