- Home /
Question by
maniac222 · Oct 21, 2011 at 09:19 AM ·
animation2dspriteplatformer
Spritesheet and 2D control
I have a code for the animation of a spritesheet. I have all the different moves of my character such as jumping, walking and running in different rows of the same image. And now ofcourse I want to change which row to animate depending on which buttons I press. How do I do this?
This is my code so far;
using UnityEngine;
using System.Collections;
public class RunCycle : MonoBehaviour
{
public int colcount = 8;
public int rowcount = 1;
int rownum = 0;
int colnum = 0;
public int totalcells = 8;
public int fps = 5;
Vector2 offset;
void Update ()
{
spriteanimation(colcount,rowcount,rownum,colnum,totalcells,fps);
}
private void spriteanimation(int colCount,int rowCount,int rowNumber,int colNumber,int totalCells,int fps)
{
// Calculate index
int index = (int)(Time.time * (float)fps);
// Repeat when exhausting all cells
index = index % totalCells;
// Size of every cell
Vector2 size = new Vector2 (1.0f / colCount, 1.0f / rowCount);
// split into horizontal and vertical index
int uIndex = index % colCount;
int vIndex = index / colCount;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
offset = new Vector2 ((uIndex+colNumber) * size.x, (1.0f - size.y) - (vIndex+rowNumber) * size.y);
renderer.material.SetTextureOffset ("_MainTex", offset);
renderer.material.SetTextureScale ("_MainTex", size);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to Change to the Same Point in Another Animation 0 Answers
Animated Overlay Armor Sprite 1 Answer
Running 2D Sprite Animation Once? 2 Answers
How to create animated "sprites" in a 3D environment 1 Answer
Animataion of a Sprite v2 1 Answer