Blog 3 :- Skeleton animation in Unity
Hey Guys, this blog is part of a series " Skeleton animation in Unity". In this, I'll try to explain "how to manage animation speed of spine objects in Unity".
For this make sure you have "spine-unity.unitypackage" plugin in your project.
It is assumed that you have already Instantiated your spine object in Game Scene and know how to play animations using code, if not please go through with previous 2blogs links are below:
1.) "How to instantiate spine objects in Unity".
2.) "How to play animations of spine objects in Unity".
Step1: Create a script as " PlayAnimation.cs" an apply it on Spine Character.
Step 2: Use following code :
We will play animations on Key press
using UnityEngine;
using System.Collections;
public class PlayAnimation : MonoBehaviour {
//assign all spine animations of game character to string variables
string idleAnimation = "1 Idle Animation";
string runAnimation = "2 Running Animation";
SkeletonAnimation skeletonAnimation; //skeleton animation variable will used to call methods of SkeletonAnimation
void Start ()
{
//assign skeletonAnimation
skeletonAnimation=GetComponent<SkeletonAnimation>();
SetAnimation(idleAnimation,true);
}
//Function to set animations to spine character or we can set animation directly without making this function.
void SetAnimation (string anim, bool loop)
{
skeletonAnimation.state.SetAnimation(0, anim, loop);
}
void Update ()
{
if(Input.GetKey(KeyCode.A))
SetAnimation(runAnimation,false);
if(Input.GetKey(KeyCode.S)) //SpeedUp animation on KeyPress
skeletonAnimation.timeScale = 2;
if(Input.GetKey(KeyCode.X)) //SpeedDown animation again on KeyPress
skeletonAnimation.timeScale = 0.5f;
if(Input.GetKey(KeyCode.U)) //Usual speed of animation again on KeyPress
skeletonAnimation.timeScale = 1;
}
}
Now you can play animations with speed management on key press of spine object.
1 Comment(s)