- Home /
Evenly distributing sprites in an animation
Is there a way to evenly distribute sprites along the timeline in an animation?
I know that I can change the sample rate to speed up/slow down the sprite animation, but this won't work for my purposes. I need to be able to create an animation of a given length, drop my sprite in that animation and ensure that they're evenly distributed within that amount of time (in order to ensure synchronization with the animation of other gameobjects being animated from the same animator).
See this related question for more info.
I'm not 100% sure what you are asking here, but I think the answer I gave in your linked question will solve both issues.
While your other post was definitely helpful, it doesn't really address this issue, unless syncing an animation layer would essentially achieve this?
I think so, but, like I said, I'm not really sure what you're asking.
Answer by phxvyper · May 20, 2016 at 10:42 PM
If I understand what you're asking, this is simple division.
Say you have 100 sprites in an animation, n = 100
; And you have an animation that is 2 seconds long, t = 2
;
In order to even distribute n sprites along an animation of t length, you have to find the dt (change in t) in which you distribute this. In order to find that period of time per sprite, you would divide t by n:
t / n = 2 (seconds) / 100 (sprites)
2 / 100 = 0.02/1 (seconds/sprites) or dt = 0.02.
Start your first sprite at t = 0, then increment t by dt for each sprite in your animation and place that sprite at that time.
e.g. sprite 5 should be at t = dt * 4 = 0.08
EDIT:
You can do math in code. The following code is basically pseudo code since it doesn't use Unity's API whatsoever, but you can apply it to whatever system you're using probably.
Since animations use a frame system, you'd have to find out a framerate:
rate = n / t = 100 / 2 = 50 fps;
and this can be applied in code:
struct Animation {
float rate;
Sprite[] frames;
public Animation(float rate, params Sprite[] frames) {
this.rate = rate;
this.frames = frames;
}
}
Animation CreateAnimationFromSprites(Sprite[] sprites, float animationLength) {
float rate = (sprites.Length / animationLength);
return new Animation(rate, sprites);
}
and all you'll have to supply is your collection of sprites, and the length of the animation.
Thanks. That's exactly what I want to do, but can it be automated?
I added new information to explain automation in code, just call that CreateAnimationFromSprites function whenever you want to create an animation - but make sure you translate it to use whatever Animation system you're implementing!
Awesome, thank you. I'll have to fiddle with this and let you know how it works
Your answer
Follow this Question
Related Questions
2d Animation sprites - right way? 0 Answers
2d sprite animation issue 2 Answers
Animating hair over sprite animation 0 Answers
Lighting an Animated Player 0 Answers
How to swap sprites? 1 Answer