Witam,mam pewien problem w skrypcie.
Chodzi mi dokładniej o to,by metoda SetSpriteAnimation wykonała się 4 razy,ale tak bym to mógł zauważyć,gdy kliknę jakiś klawisz,WSAD kilkukrotnie niestety to wykonuję się jedynie 1/4 całej animacji przeznaczonej na ten klawisz.Jednak gdy wklejam 4 razy w jedno miejsce tą metodę to niestety nie ma żadnych zmian.Co źle robię ?
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)|| Input.GetKey(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 );
}
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);
}
}