- Home /
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: https://youtu.be/h_o8AnZX9FA?t=19s
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?
Answer by UnityCoach · Jan 03, 2017 at 10:51 AM
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));
I've set up an animation curve though I'm just not sure how to implement
result = $$anonymous$$athf.Lerp ($$anonymous$$, max, curve.Evaluate (position));
into my script and have it, of course work correctly.
Would I use that statement for both the X and Y axis of sonic? Since what I'm doing in my current script is adding onto sonic's x and y velocity by 0.5 depending on the position of the player.
Yes, I believe, you need two curves, or maybe just one if you want the same behaviour on X and Y.
I was going to use one curve for both the x and y axis though I'm not sure if that's best to get the same movement sonic has in the actual game.
I'm still unsure how to implement that into my script though, I've done it like this but that clearly wrong as it doesn't work XD:
if (transform.position.x > player.transform.position.x)
{
//flyV.x -= xa;
flyV.x = $$anonymous$$athf.Lerp(-maxSpeed, maxSpeed, flyCurve.Evaluate(transform.position.x));
}
else
{
//flyV.x += xa;
flyV.x = $$anonymous$$athf.Lerp(-maxSpeed, maxSpeed, flyCurve.Evaluate(transform.position.x));
}
if (transform.position.y > player.transform.position.y)
{
//flyV.y -= ya;
flyV.y = $$anonymous$$athf.Lerp(-maxSpeed, maxSpeed, flyCurve.Evaluate(transform.position.y));
}
else
{
//flyV.y += ya;
flyV.y = $$anonymous$$athf.Lerp(-maxSpeed, maxSpeed, flyCurve.Evaluate(transform.position.y));
}
The flyV variable is sonic's velocity and the maxSpeed variable is the max speed sonic can reach in both directions.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Super Sonic Movement Help 1 Answer
Return Proper Velocity of Player object 3 Answers
Prevent Object/Character from "drifting" away from Collider 1 Answer
Bullet not moving from script 3 Answers