- Home /
Animating 2D sprite upon movement script help.
Hi, I can't seem to get my script working, which is meant to animate my sprite upon movement. Can anybody tell me what is wrong with this script? I have all of the images in the right place, it just seems to animate even when not moving.
Here is the script:
public var frameSpeed:float=0.05;
private var nextFrame:float=0;
public var sprites:Texture2D [];
private var counter:int=0;
function Update ()
{
if (Input.GetKey(KeyCode.UpArrow))
{
this.transform.Translate(Vector3.left*5*Time.deltaTime);
changeTexture ();
}
if (Input.GetKey (KeyCode.DownArrow))
{
this.transform.Translate (-Vector3.left*5*Time.deltaTime);
changeTexture ();
}
if (Input.GetKey (KeyCode.LeftArrow))
{
this.transform.Rotate(-Vector3.up*100*Time.deltaTime);
}
if (Input.GetKey (KeyCode.RightArrow))
{
this.transform.Rotate (Vector3.up*100*Time.deltaTime);
}
this.renderer.material.SetTexture ("_MainTex", sprites[counter]);
counter++;
if (counter==sprites.Length) counter=0;
}
function changeTexture ()
{
this.renderer.material.SetTexture("_MainTex", sprites[counter]);
if (Time.time>nextFrame)
{
counter++;
nextFrame=Time.time+frameSpeed;
}
if (counter==sprites.Length) counter=0;
}
Almost all of the code will only be executed on key input, the only thing that looks a little suspicious to me are the lines (In Update):
this.renderer.material.SetTexture ("_$$anonymous$$ainTex", sprites[counter]);
counter++;
if (counter==sprites.Length) counter=0;
seeing as they are being run each frame, might be totally off though..
Answer by joseaguirre · Feb 22, 2014 at 12:35 AM
This is from a similar question i asked a while ago
var uvAnimationTileX = 24; //Here you can place the number of columns of your sheet.
//The above sheet has 24
var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet.
//The above sheet has 1
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);
}
Answer by jpsmarinho812 · Feb 22, 2014 at 04:02 AM
I think this is old. The best way is to use Animation Controller and Animation:
http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-overview
Your answer
Follow this Question
Related Questions
Sprite with non-constant jitter during constant velocity movement. 2 Answers
When i make my character animated, he can only move up, down left and right (2D) (JS) 0 Answers
2D Game Movement script(W,A,S,D input only)? 1 Answer
Mouse Click Based Movement/Actions & 2D Sprite Rotation in 3D Space 0 Answers
[2D] Camera movement causes flickering/jittering sprites 2 Answers