- Home /
How do I stop sprite animation on last frame?
Hi there, I have been using this script to animate a sprite sheet and I was wondering... How would I pause the animation on the last frame? I'm trying to do this for a falling animation. I've tried everything I can think of and spent hours trying to solve this myself. Can anyone out there help me out a bit? Thanks in advance for any answer provided!
Code:
function aniSprite (columnSize, rowSize, colFrameStart, rowFrameStart, totalFrames, framesPerSecond){
var index : int = Time.time * framesPerSecond; // time control fps
index = index % totalFrames; // modulate to total number of frames
var size = Vector2 ( 1.0 / columnSize, 1.0 / rowSize); // scale for column and row size
var u = index % columnSize; // u gets current x coordinate from column size
var v = index / columnSize; // v gets current y coordinate by dividing by column size
var offset = Vector2 ((u + colFrameStart) * size.x,(1.0 - size.y) - (v + rowFrameStart) * size.y); // offset equals column and row
GetComponent(Renderer).material.mainTextureOffset = offset; // texture offset for diffuse map
GetComponent(Renderer).material.mainTextureScale = size; // texture scale for diffuse map
}
Why not use the built in sprite editor & animator ins$$anonymous$$d of offsetting the sprite manually? https://unity3d.com/learn/tutorials/projects/2d-roguelike/animations
This way you can trigger an animation by setting a boolean and when that boolean is no longer true you can go back to whatever state you want without having to write the business logic for animating the sprites and transitions from one state to the other
Thanks... I did not know about that animator. (It looks fantastic!) Is there a tutorial out there on how to crop the sprites into individual frames?
Answer by virgiliu · May 27, 2015 at 12:27 PM
Here you go https://unity3d.com/learn/tutorials/modules/beginner/2d/sprite-editor
http://docs.unity3d.com/460/Documentation/Manual/SpriteEditor.html
If you have the time watch this tutorial, it has basically everything you need to understand the sprite editor and animation and has some even more complex things like animation blend trees
https://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers
Thanks again for all your help! The new animation tool is easy and fast!
Answer by bibleboy4u · May 28, 2015 at 06:55 AM
The comment above solved my problem...
I converted it to an answer if you'd like to accept it :)
Your answer
Follow this Question
Related Questions
Setting animator parameter on a single instance of a prefab sets the parameter for all instances 3 Answers
Sprites: Project Sprite to Mesh, allow for bending of sprites for 3d environments 0 Answers
Using same animation with different sprite sheets 0 Answers
Sprite Rect Moves Between Frames 0 Answers
Extra frame in simple animations? 0 Answers