- Home /
Unity 2d toolkit, play specific clip
I am trying to play a specific clip on input. I want it to change clips, from the idle (it starts out as this clip) to the Begin Run clip. I have a animated sprite with both of these clips included. I think there is a simple syntax for this but I just can't figure it out.
I've tried this code, John is the name of the player object,
function Update (){
if(Input.GetKey == "d"){
player.transform.position += Vector3(forwarddirection, 0,0);
John.Play ("Begin Run");
}
}
I've tried 2dTK's example for playing specific clips
function Update (){
if(Input.GetKey == "d"){
player.transform.position += Vector3(forwarddirection, 0,0);
Tk2dAnimatedSprite.Play ("Begin Run");
}
}
I've even tried the name of my animated sprite
function Update (){
if(Input.GetKey == "d"){
player.transform.position += Vector3(forwarddirection, 0,0);
AnimatedSprite.Play ("Begin Run");
}
}
None of it works. Should I be using a static sprite batcher instead of an animated sprite? please help.
Answer by rednax20 · May 14, 2013 at 12:27 AM
I found out my problem. I have to define a variable not just use tk2danimatedsprite. for those that have my problem, I will post this so that you can get help
var animatedSprite : tk2dAnimatedSprite;
animatedSprite = gameObject.GetComponent(tk2dAnimatedSprite);
animatedSprite.Play("begin run");
I hope this helps, Ithe problem also might have been that I use javaScript, and t2tk uses c. Hope this helps
I forgot to note, you must configure for java script before this will work in java script. That was another problem I had.
hi im having same problem I think
what do u mean define a variable which variable please help
im having troubles and cant figure out
hello ger, this question was posted over a year and a half ago. I will try to help you as best i can but I may have forgotten a lot of my unity 2d toolkit knowledge.
first off screw everything i said in my question I used to be a complete idiot back then, (judging by what i typed) if your doing that stuff above, don't.
so when you want to play an animation from a sprite you must define the variable for the sprite
var bazooka : tk2dAnimatedSprite;
bazooka = gameObject.GetComponent(tk2dAnimatedSprite);
essentially this will now represent the animated sprite.
Have you learned how to assign game objects in the inspector? its the same thing but through code. If you want to you can type
var bazooka : tk2dAnimatedSprite;
and then drag the animated sprite onto this in the inspector. it will do the exact same thing
whenever you type 'bazooka' you are now referencing the sprite and its animations. so now you can type
bazooka.play("whatever animation you could possibly want");
anywhere in the script and the animation will play, inside a for loop, an if statement, update, whatever.
and remember if you ant to use 2dtk with javascript like this you will have to convert to javascript.
hope this helps, if you need more help i'd be happy to give more assistance, just give me more information if you are still stuck.
and it may take me a while to get back to you, don't worry though, i will eventually get back to you
Your answer
Follow this Question
Related Questions
Noob Question: Play Specific Audio 1 Answer
How to play an animation in reverse? 1 Answer
Sprite Touch and Drag in 2dToolkit 1 Answer
Sprite collor don't change on animation 0 Answers
Audio Clip Plays 1 turn too late 1 Answer