Blog 2 :- 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 play animations 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 Instatntitated your spine object in Game Scene.
Step1: Create a script as "PlayerAnimations.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
stringidleAnimation="1IdleAnimation";
stringrunAnimation="2RunningAnimation";
stringjumpAnimation="3JumpingAnimation";
stringattackAnimation="4Attack";
SkeletonAnimationskeletonAnimation; //skeleton animation variable will used to call methods of SkeletonAnimation
voidStart()
{
//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.
voidSetAnimation(stringanim,boolloop)
{
skeletonAnimation.state.SetAnimation(0,anim,loop);
}
voidUpdate()
{
if(Input.GetKey(KeyCode.A))
SetAnimation(runAnimation,false);
if(Input.GetKey(KeyCode.W))
SetAnimation(jumpAnimation,false);
if(Input.GetKey(KeyCode.Space))
SetAnimation(attackAnimation,false);
if(Input.GetKey(KeyCode.I))
SetAnimation(idleAnimation,false);
}
}
Now you can play animations on key press of spine object.
Comment for suggestions.
Thank You !
0 Comment(s)