# Using Animation Curve for Movement Help

I’m trying to recreate the sonic boss fight from the game “I wanna be the Boshy” just for fun and for learning purposes, currently I’m recreating this attack: I Wanna Be The Boshy -- Sonic [IWBTB] - YouTube

Currently I’ve scripted the boss fight like this:

``````public void SuperFly(float speed, float maxSpeed)
{
float xa = speed;
float ya = speed;

flyV.x = Mathf.Clamp(flyV.x, -maxSpeed, maxSpeed);
flyV.y = Mathf.Clamp(flyV.y, -maxSpeed, maxSpeed);
rig.velocity = flyV;

Vector2 v = rig.velocity;
float angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, transform.forward);

if(!runningState) //Start Of State
{
stateTimer = 30f;
anim.SetTrigger("SuperFly");
transform.position = transformPoint.transform.position;
ChangeDirection("Right");
flyV = rig.velocity;
runningState = true;
}

if (transform.position.x > player.transform.position.x)
{
flyV.x -= xa;
}
else
{
flyV.x += xa;
}

if (transform.position.y > player.transform.position.y)
{
flyV.y -= ya;
}
else
{
flyV.y += ya;
}
}
``````

and it gives sort of similar movement to the movement in the actual game: http://i.imgur.com/ctjL3lF.gif (Not a good recreation, still got a lot to do). If you compare the movement of sonic from my version to the actual game version then you can notice there something different about it, sonic’s movement in the actual game seems for quick and sharp compared to my version, I actually created a topic a couple of days ago asking about this and someone gave me this: https://s-media-cache-ak0.pinimg.com/originals/6c/1b/a4/6c1ba40bf194cd0fc99c06207246b51f.gif

The thing is I’m just not sure how I can implement that into my script and I was wondering if someone would help me?

You can use Animation Curves.
Simply expose a parameter of type AnimationCurve, set it up in the Editor, then in the code, use curve.Evaluate (position) to get the value at the given position.
In these cases, I always make my curves go between 0 and 1 on both time and value, then I usually use the result in a Lerp if I want it to go beyond that.

like :

``````result = Mathf.Lerp (min, max, curve.Evaluate (position));
``````