Hi,I want to run the SpriteAnimation 4 times for one button click.
I have this Script:
using UnityEngine;
using System.Collections;
public class PlayerEssentials : MonoBehaviour {
// Use this for initialization
private double speed = 10.0;
private Vector3 pos;
private Transform tr;
public int colCount = 4;
public int rowCount = 4;
public Vector2 MoveVector;
public int rowNumber = 0;
public int colNumber = 0;
public int totalCells = 4;
public int fps = 10;
void Start () {
tr = transform;
pos = tr.position;
SetSpriteAnimation( colCount, rowCount, rowNumber, colNumber, totalCells, fps );
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.D))
{
for(int i = 0;i < 4;i++)
{
pos += Vector3.right;
}
rowNumber = 2;
colNumber = 0;
totalCells = 4;
SetSpriteAnimation( colCount, rowCount, rowNumber, colNumber, totalCells, fps );
SetSpriteAnimation( colCount, rowCount, rowNumber, colNumber, totalCells, fps );
}
else if (Input.GetKeyDown(KeyCode.A) && tr.position == pos) {
rowNumber = 1;
colNumber = 0;
totalCells = 4;
pos += Vector3.left;
SetSpriteAnimation( colCount, rowCount, rowNumber, colNumber, totalCells, fps );
}
else if (Input.GetKeyDown(KeyCode.W) && tr.position == pos) {
pos += Vector3.up;
rowNumber = 3;
colNumber = 0;
totalCells = 4;
SetSpriteAnimation( colCount, rowCount, rowNumber, colNumber, totalCells, fps );
}
else if (Input.GetKeyDown(KeyCode.S) && tr.position == pos) {
pos += Vector3.down;
rowNumber = 0;
colNumber = 0;
totalCells = 4;
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * (float)speed);
}
void SetSpriteAnimation(int colCount ,int rowCount ,int rowNumber ,int colNumber,int totalCells,int fps )
{
int index = (int)(Time.time * fps);
index = index % totalCells; // => 0 1 2 3 / 0 1 2 3 / 0 1 2 3 …
float sizeX = 1.0f / colCount;
float sizeY = 1.0f / rowCount;
Vector2 size = new Vector2 (sizeX, sizeY);
var uIndex = index % colCount;
var vIndex = index / colCount;
float offsetX = (uIndex + colNumber) * size.x;
float offsetY = (1.0f - size.y) - (vIndex + rowNumber) * size.y;
Vector2 offset = new Vector2 (offsetX, offsetY);
renderer.material.SetTextureOffset (“_MainTex”, offset);
renderer.material.SetTextureScale (“_MainTex”, size);
}
}
I don’t know how to modify this script to see the result of walking (One walking SpriteAnimation has 4 images in 1 image but when I’m clicking the button then the Animation is doing only 1 image) ![]()