- Home /
Animated Sprite Usage
Hey guys im a but confused as to how to create and utilize sprites in Unity. I can't seem to find a clear source of information online.
Let's say i download this png. http://opengameart.org/sites/default/files/zombie_topdown.png
Now i import it into unity, select sprite mode: multiple, and manually cut each zombie image.
Can that work, or do i need to have every type of animation as a separate sprite with option sprite mode: multiple?
How can i specify for the gameObject to play the "walking" animation when a zombie is walking, play "dying" animation when it gets hit?
In the classic 3D animator things were simple, u just have a different clips and u basically do animator.play(), but with this sprite stuff im confused.
UPDATE: OH ONE MORE QUESTION!
I see in the sprite sheet all the different orientations of zombies (upleft up upright right downright down downleft left)
I don't understand why that's necessary, can we just rotate by script one version of the sprite to achieve the right direction, or is doing it this way much more convenient somehow? can someone please explain how, thanks :)
Im seeing in the 2D example project the usage of controllers and clips.
The swan in the project is a sprite sheet, and its animated using the Animator component with a "swanFly" controller and a "swanFly" clip.
Why is there no tutorial for how to work this stuff?? i really wanna learn how to use it!
ok starting to understand this slowly...
the attached animator contains the controller, and the clips are played in the animator window showing a state machine.
now i gotta figure out how to make the actual clips using the tons of sprites that were generated
Also what's the difference between adjusting sprite size using scale and Pixels To Units?
Answer by Spinnernicholas · Dec 26, 2013 at 05:46 PM
There are two Animation Controller Components built into Unity.
The first is the Animation component. That is the one that allows animation.play("animationName"). It, unfortunately, is legacy now and doesn't support the new sprite "dope sheet" animation.
The second component is the Animator component. The Animator component is more robust, but also more complicated. If your animation is tied to an Animator component, then you can use the Dope Sheet in the Animation window. That gives you the desired type of animation.
You can roughly replicate the Animation component's animation.play("animationName") functionality with the Animator component. To do this, in the animation controller, create a state and a trigger for each animation. Then, create transitions with the triggers from the special "ANY" state to the state for the animation you want to trigger. Then, you can play the animation like this.
getComponent<Animator>().SetTrigger("animationTriggerName");
for more information on how to use the Animator, look HERE.
yes i was doing some learning yesterday and i pretty much got the workflow sorted out in my head.
I get how the transitions are controlled the defined variables which are themselves controlled through scripting. It's actually a great workflow cause the state machine really lets you see clearly the logical animation transitions.
Answer by azmat786n · Dec 24, 2013 at 06:55 PM
Add Sprite sheet or Single Sprites to unity project and
for animation you have to use your own script i am sharing that script below
var uvAnimationTileX = 24; //Here you can place the number of columns of your sheet.
var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet.
var framesPerSecond = 10.0;
function Update () {
// Calculate index
var index : int = Time.time * framesPerSecond;
// repeat when exhausting all frames
index = index % (uvAnimationTileX * uvAnimationTileY);
// Size of every tile
var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
// split into horizontal and vertical index
var uIndex = index % uvAnimationTileX;
var vIndex = index / uvAnimationTileX;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);
renderer.material.SetTextureOffset ("_MainTex", offset);
renderer.material.SetTextureScale ("_MainTex", size);
}
And for single sprites
var frames : Texture2D[];
var framesPerSecond = 10.0;
function Update () {
var index : int = Time.time * framesPerSecond;
index = index % frames.Length;
renderer.material.mainTexture = frames[index];
}
Hope This is helpful for you...
oooh awesome dude.
I imported the image and used the automatic slicer which worked awesomely. It generated a different file for each image in the large one with generic na$$anonymous$$g like:
image1 image2 image3 ... ...
So if i use ur single sprites code, Can i basically have have multiple arrays, one for walking, one for dying, etc... and manually drag the right textures into them