- Home /
Animataion of a Sprite v2
Hey Unity Community, I am trying to animate this sprite, in this scene. I have asked this question before, and some cool people helped me to get started.
For some reason nothing is happening when I play the game.
This is the sprite sheet:
To make the sprite move, it has to increase the OffsetX from (0,0.33,0.66) and decrease the offsetY from (0.66,0.33,0). When the coordinates get to X = 0.66, and the Y is at zero, there is nothing there, so I just reset the coordinates back at the first sprite.
Here is the code:
using UnityEngine;
using System.Collections;
public class SpriteSheet_Asuka : MonoBehaviour
{
// Use this for initialization
//OffsetX and offsetY to set where it the sprite starts.
public float offsetX = 0f;
public float offsetY = 0.66f;
public float tilingX = 0.3f;
public float tilintY = 0.3f;
void Start()
{
animate();
}
// Update is called once per frame
void Update()
{
renderer.material.mainTextureOffset = new Vector2(offsetX, offsetY);
renderer.material.mainTextureScale = new Vector2(tilingX, tilintY);
}
IEnumerable animate()
{
int currentFrames = 0;
int totalFrames = 7;
while (totalFrames > currentFrames)
{
renderer.material.mainTextureOffset = new Vector2(offsetX, offsetY);
if (offsetX > 0.66f)
{
offsetX = 0f;
offsetY -= 0.33f;
}
else if (offsetY == 0f && offsetX == 0.66f)
{
offsetX = 0f;
offsetY = 0.66f;
}
else
{
offsetX += 0.33f;
currentFrames++;
}
if (currentFrames == 7)
{
currentFrames = 0;
}
}
yield return new WaitForSeconds(1);
}
}
hey $$anonymous$$G3,
this is already looking much better then your version one, but still needs a bit of work.
the scale function should only be called once, so moved that into your start function, and u dont need to update the offset in the the update cause you already doing it in the animate function.
also im a bit confused why yoiu starting off with an already offset, but you not starting with currentframe = 7.
one last thing, is this a idle animation?are u hooping to loop the animation or its a 1 shot animation where u do all the frames then stop ?
Yes, that is exactly what I am trying to do. this is a idle animation, where I do all the frames then stop, in a loop, when the character is standing still.
ok as robertbu suggested u need to call your animation using startCoroutine, after this u will ahve to come up with a better way to handle the animation none stop. i would recommend putting some sort of bool IsAnimating, and have it set to true when u are in your while loop, and then in your update functino have a simepl if (IsAnimate == false) then animate away.
but again keep in $$anonymous$$d, im not sure why you have a starting offset and your starting frame is set to 0
The offset is the sprite sheet's coordinates, so at the X = 0, and y = 0.66 the offset is the top of the sprite sheet.
Answer by robertbu · Jul 16, 2013 at 02:53 PM
You need to use StartCoroutine() in C#. Line 17 should be:
StartCoroutine(animate());
Coroutines need to be of type 'IEnumerator' not 'IEnumerable', so line 31 should be:
IEnumerator animate()
Your 'yield' needs to be inside your while() loop. So move it from line 62 to line 60. If you don't do this, you will hang Unity.
I think there are some issues with your step logic for each frame, but these three changes will get you up and running.
Your answer
Follow this Question
Related Questions
SpriteManager 2 1 Answer
Sprite animation 2 Answers
How to make not smooth animation? 0 Answers