Skip to content
Permalink
3a30942fd4
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
137 lines (96 sloc) 3.48 KB
using UnityEngine;
using System.Collections;
public class Anim2D : MonoBehaviour {
// calculation variables
private Vector3 tempVector;
private Vector3 tempVector2;
private int i;
// animation variables (variables used for processing aniamtion)
public float sheetRows = 5; // Total columns in sprite sheet
public float sheetColumns = 4; // Total Rows in sprite sheet
public float FrameRate = 11f; // how many frames to play per second
private float frameNum = 1; // the current frame being played,
private string currentAnim; // the ID of the current animation being played
private float currentStart = 0; //Dynamic variable that tells the animation system what the first frame of the current animation is
private float currentEnd = 0;
private bool loopCheck; //Does this animation loop?
private float AnimTime = 0f; // time to pass before playing next animation
private Vector2 sheetPos; // the X, Y position of the frame
private Vector2 sheetOffset; // the offset value of the X, Y coordinate for the texture
//This Class allows us to create a bunch of data and store it in this specific container
[System.Serializable]
public class AnimClass
{
public string Name;
public bool looping;
public int anim_Min;
public int anim_Max;
}
public AnimClass[] Animz;
// Use this for initialization
void Start () {
//Telling this controller that Idle is the default animation so that it doesn't throw an error
currentAnim = "Idle";
}
void Update () {
HandleAnimation();
KeyCommands();
}
void HandleAnimation () // handles all animation
{
FindAnimation(currentAnim);
ProcessAnimation();
}
//Here we decide
void FindAnimation (string currentAnim)
{
for (var i = 0; i < Animz.Length; i++){
if ( Animz[i].Name == currentAnim){
loopCheck = Animz[i].looping;
currentStart = Animz[i].anim_Min;
currentEnd = Animz[i].anim_Max;
i = Animz.Length;
}
}
}
//These are dummy key input sensors. You should have a seperate player controller (or AI controller) that declares "currentAnim" to this script
void KeyCommands(){
if (Input.GetKeyDown(KeyCode.UpArrow)){
currentAnim = "Idle";
}
if (Input.GetKeyDown(KeyCode.RightArrow)){
currentAnim = "Kill";
}
}
void ProcessAnimation ()
{
// AnimTime -= Time.deltaTime; subtract the number
// of seconds passed since the last frame, if the game
// is running at 30 frames per second the variable will
// subtract by 0.033 of a second (1/30)
AnimTime -= Time.deltaTime;
if (AnimTime <= 0)
{
frameNum += 1;
// one play animations (play from start to finish)
frameNum = Mathf.Clamp(frameNum,currentStart,currentEnd+1);
if ( loopCheck == true){
if (frameNum > currentEnd){
frameNum = currentStart;
}
}
// if the FrameRate is 11, 1/11 is one eleventh of a second, that is the time we are waiting before we
//play the next frame.
AnimTime += (1/FrameRate);
}
sheetPos.y = 0;
// find the number of frames down the animation is and set the y coordinate accordingly
for (i=(int)frameNum; i > 5; i-=5)
{
sheetPos.y += 1;
}
sheetPos.x = i - 1; // find the X coordinate of the frame to play
sheetOffset = new Vector2(1 - (sheetPos.x/sheetRows),1 -(sheetPos.y/sheetColumns)); // find the X and Y coordinate of the frame to display
renderer.material.SetTextureOffset ("_MainTex", sheetOffset); // offset the texture to display the correct frame
}
}