- Home /
Animataion of a Sprite
Hey Unity Community, I am trying to animate this sprite, in this scene.
I know Using sprite Manager is the way to go, although. When I did the code, I did not do it correctly, simply cause I could not figure out my Pixel Coordinates, Spacing, UV Spacing. Accurate enough. And when it animates, it was too large.
This is the newly created Sprite Sheet I made with Texture Packer I just gotten.
I have not tried it out with Sprite Manager. So I tried to create my own animation code.
which was this:
using UnityEngine;
using System.Collections;
public class SpriteSheet : MonoBehaviour {
public int numFrame = 6;
public int currentFrame = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
transform.position = new Vector3(0f, 0f, 0.01f);
}
IEnumerable Wait()
{
yield return new WaitForSeconds(1);
print("Wait" + Time.time);
}
public void animate()
{
float offset = 0;
offset += 1.0f / numFrame;
renderer.material.mainTextureOffset = new Vector2(offset,0);
currentFrame += 1;
while (currentFrame > numFrame)
{
offset = 0;
renderer.material.mainTextureOffset = new Vector2(offset, 0);
currentFrame = 1;
}
}
}
This code makes the sprite goes to the origin, for some reason when it runs, instead of messing with the Material Offset, But actually I think I am messing with the particle offset.
I Have 2 questions:
How can I easily find my Pixel Coordinates, UV corrdinates, and spacing for both using SpriteManagers. with PixelSpaceToUVSpace() and PixelCoordToUVCoord() in examples.
Else
How can I get this Animation code do do what I want, or at least some tips?
im having a hard time understanding the question.
just looking at the code, im unsure how the logic goes. you have a yield function setup, but u never use it. and it the animate function, the while loop would run its whole iteration each frame, so you will always end up with only the last frame.
if you want to use the yield function, im guessing you would want to to put it into the while loop. so that each time the while loop iterates once it then waits, 1 second is proabaly to long. this is prob not exact as im not at home, but i think u want
public IEnumerable animate()
{
float offset = 0;
offset += 1.0f / numFrame;
renderer.material.mainTextureOffset = new Vector2(offset,0);
currentFrame += 1;
while (currentFrame > numFrame)
{
offset = 0;
renderer.material.mainTextureOffset = new Vector2(offset, 0);
currentFrame = 1;
yield return new WaitForSeconds(1);
}
}
I altered the code from this tutorial: http://www.youtube.com/watch?v=igxi8d2f5bc.
So I was kinda of confused on what he was doing.
If anything I wanna use Sprite$$anonymous$$anager. but I am just trying things out. I'll take your advice and keep hacking at it. I a just learning what the yield does as well
ya , your use of yield is not correct , once u wrap your brain around it , it will be simple to see what to do.
looking at the function, it needs a bit more work , as u want to loop the current frame over the total numb of frames, then figure out your offset in the while loop
mostly psudo code, but the idea should get you moving, i added a magic function that u need to figure out how to write
public IEnumerable animate()
{
int totalFrames = 8;
int currentFrame = 0;
while (currentFrame > numFrame)
{
offsetX = getOffset("x"); // this is tricky as u need to offset on the x and y. using mod for this is easy but hard to understand, let me know if u cant figure it out
offsetY = getOffset("y");
renderer.material.mainTextureOffset = new Vector2(offsetX, offsetY);
currentFrame += 1;
yield return new WaitForSeconds(1);
}
}