Permalink
Cannot retrieve contributors at this time
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?
SpaceKitten/Assets/Scripts/Anim2D.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
91 lines (74 sloc)
2.82 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Anim2D : MonoBehaviour { | |
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, | |
[HideInInspector] | |
public 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; | |
void Start() { | |
//Telling this controller that Idle is the default animation so that it doesn't throw an error | |
currentAnim = "Idle"; | |
} | |
void Update() { | |
FindAnimation(currentAnim); | |
ProcessAnimation(); | |
} | |
//Here we decide | |
void FindAnimation(string currentAnim) { | |
for (int 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; | |
} | |
} | |
} | |
public void SetAnim(string anim) { | |
currentAnim = anim; | |
} | |
public string GetAnim() { | |
return currentAnim; | |
} | |
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 = Mathf.Floor(frameNum/sheetRows); | |
sheetPos.x = (frameNum % sheetRows) - 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 | |
} | |
} |