- Home /
Sprite Sheet animation without fancy add-ons
Hi all,
I am trying to include some sprite animations in my 2D beat-em-up/action game. I have glanced over many of the 2d sprite management add-ons in the unity store but alas I am very poor. I do not think I will use them enough to justify purchasing them. So, despite having no experience with animation, I decided to try animating sprite sheets on my own.
It has been going well so far. I have had success with basic animations upon button click and others that cycle through the sheet over time. But I've run into a problem. My artist has decided to add some frames on the basic attack animations and it no longer seems to be working the way I intended.
Here is a working version of the current animations. This is the main character's three-hit combo string:
if (Input.GetButtonDown("fight"))
{
atktimer = true;
atkreset = 0;
if (attackcounter <= 3)
attackcounter++;
StartCoroutine (PlayAnimation () );
if (attackcounter > 3)
attackcounter = 0;
}
if (atktimer)
{
atkreset += Time.deltaTime;
if (atkreset - Time.deltaTime > 2f)
{
atktimer = false;
attackcounter = 0;
}
}
IEnumerator PlayAnimation ()
{
//attack1
if (attackcounter == 1)
{
animating = true;
audio.PlayOneShot(whoosh1);
renderer.material.mainTextureOffset = new Vector2 (0.25f, 0.5f);
yield return new WaitForSeconds(0.5f);
animating = false;
frameanimation = 0f;
}
//attack2
if (attackcounter == 2)
{
animating = true;
audio.PlayOneShot(whoosh2);
renderer.material.mainTextureOffset = new Vector2 (0.75f, 0.5f);
yield return new WaitForSeconds(0.5f);
animating = false;
}
//attack3
if (attackcounter == 3)
{
animating = true;
renderer.material.mainTextureOffset = new Vector2 (0.75f, 0.25f);
yield return new WaitForSeconds(0.5f);
animating = false;
}
}
What this does is: Every time I click the attack button, the main character cycles through 3 different images on the sprite sheet, and at the end, it returns to the first one.
What I need: Since each attack now has additional frames, instead of simply changing the renderer offsets once per click, it needs to go through 2 frames before I click the button again. This is what I have attempted to solve the problem:
In update:
if (animating)
{
frametimer += Time.deltaTime;
}
And in my Playanimation method:
//attack1
if (attackcounter == 1)
{
animating = true;
audio.PlayOneShot(whoosh1);
if (frametimer - Time.deltaTime > 0)
renderer.material.mainTextureOffset = new Vector2 (0f, 0.5f);
if (frametimer - Time.deltaTime > 0.25f)
renderer.material.mainTextureOffset = new Vector2 (0.25f, 0.5f);
yield return new WaitForSeconds(0.5f);
animating = false;
frametimer = 0f;
}
As you can see, I made a new timer to keep track of the smaller frames and inserted it into the larger attack command. It should switch frames as frametimer increments itself and play each frame for 0.25 seconds each. But it does not do this. Perhaps, there is some error in my structure or logic?
Thanks for reading! I would appreciate any help or general advice on sprite animation.
BTW there is a free sprite sheet manager called Othello
Answer by whydoidoit · Jun 18, 2012 at 08:01 PM
So you need to put a yield return new WaitForSeconds(0.25f) between your frames:
if (attackcounter == 1)
{
audio.PlayOneShot(whoosh1);
//Frame 1
renderer.material.mainTextureOffset = new Vector2 (0f, 0.5f);
yield return new WaitForSeconds(0.25f);
//Frame2
renderer.material.mainTextureOffset = new Vector2 (0.25f, 0.5f);
yield return new WaitForSeconds(0.25f);
//Frame3
renderer.material.mainTextureOffset = new Vector2 (0.5f, 0.5f);
yield return new WaitForSeconds(0.25f);
renderer.material.mainTextureOffset = new Vector2(0f,0f); //Or whatever is the default texture
}
It strikes me you would be better off always laying out your animations in blocks though so you can cycle through the texture. Write yourself a functoin to get the UV of the texture at a given "Cell" and then you just need to iterate over cells and call that function.
Your answer
Follow this Question
Related Questions
2D Character Animation Help 1 Answer
How do I flip a 3D object to look towards left or right on a 2D plane 0 Answers
2d Beat Em Up Jump Functionality 0 Answers
Co-routines, timing and framerate independence 2 Answers
Multiple Cars not working 1 Answer