- Home /
Animation for Powerup Won't Work?
Hey guys, it's been awhile since I've ask a question. I'm having a problem with my script that supposed to play an animation after I grab a power up. It won't work and I can't figure out why.
public class AnimTest : MonoBehaviour {
GameObject BoostPowerupSpawn;
void OnTriggerEnter2D ( Collider2D Other ){
if(Other.gameObject.tag == "Player"){
BoostPowerupSpawn.animation.Play("Boost");
}
}
}
"BoostPowerupSpawn" is the name of the gameobject for the power up. "Player" is the name of the tag for the player/character. "Boost" is the animation I want to play. Both the power up and the player have animator attached to them. When I put this script on my power up, it looks like this. When I tried to put it on my player, the BoostPowerupSpawn changes to Character, the tag Player changes to Item, and the name of the animation changes to "Death"(since I don't have a boost animation attached to the player yet because this is a test). Please help me. By the way, "Boost" is not in the animator, but I don't see how that affects anything since Death is in the animator yet that doesn't work.
(Referring to the use of a bool) I could possibly use a trigger too, but I don't know if that would work for me.
Answer by RyanPaterson · Jun 19, 2014 at 12:18 AM
Hey, i'm not too knowledgeable on mecanim but I might be able to help with one or two things.
First thing I noticed:
You said your death animation doesn't play? It'll be because there is no transition to it. Create a transition from 'Any State' to your 'death' state
I'm not sure at all weather you can use animations as well asing animators.. however I don't see why you'd want to. You could also transition from 'Any State' or something to 'Boost'
and you could set it to a bool e.g. "boost" = false; *In the animator panel Then in script you would access it like this:
anim.SetBool ( "boost" , true);
anim being a reference to the Animator Component on the object
Which can be found, and initialised in the awake function
e.g.
Animator anim = GameObject.FindObjectWithTag("Player").GetComponent();
That being said, i've never used 2D either, so maybe its different. Either way, give it a try and good luck
Also: All code is untested just off the top of my head, there may be spelling errors.
Thanks a lot for your help. I have some questions though. Do I combine the "anim.SetBool ( "boost" , true)" and the "Animator anim = GameObject.FindObjectWithTag("Player").GetComponent();" with my script that I posted. Or should I create a whole new one?
The script you posted is fine, it's just missing a few things. Your script needs to be able to talk with the animator controller. Without the link between the two, your animation controller will only display the idle animation. Because it hasn't been manipulated to do otherwise.
The way you do that is by creating a instance of it, this is so far an empty instance
Animator anim;
Then, you need to find the component on the object and add it to the instance, this can be done in the awake function:
void Awake()
{
anim = GameObject.FindObjectWithTag("Player").GetComponent<Animator>();
}
Note: It has to point to the specific object, via a specific tag. (If you get errors with this, it's most likely a tag hasn't been properly set, or the object is missing the component your trying to locate.
When you use something like
anim.SetBool("boost", true);
it's just like you would use a normal bool, but it's linked to the variables you created in your animation controller. (Like how you currently have (speed, ground, vSpeed).
So you could have it all in one script ilke this: Note: I haven't checked this for spelling or syntax errors
public class AnimTest : $$anonymous$$onoBehaviour {
GameObject BoostPowerupSpawn;
Animator anim;
void Awake()
{
anim = GameObject.FindObjectWithTag("Player").GetComponent<Animator>();
}
void OnTriggerEnter2D ( Collider2D Other ){
if(Other.gameObject.tag == "Player"){
anim.SetBool("Boost", true);
}
}
}
*Also, if you are happy with the answers I've given, could you accept the answer I posted as an answer? Thanks
Where should I put the script at? On my player or BoostPowerupSpawn? I ask this because it's not working correctly for me. It might have something to do with the bool in the animator. Oh and I changed the code "anim = GameObject.FindObjectWithTag("Player").GetComponent();" to this:
anim = GameObject.FindGameObjectWithTag("Player").GetComponent();
Here's some screen shots of the animator:
The boost animation plays right when I start the game, not when I grab the power up. So how can I fix this? Thanks for all your help. By the way the name "Boost" where it says conditions on the seconds image, is the bool.
I've accepted your answer as you asked, but I am not happy/satisfied with what is left of your answers(I haven't solved my problem yet). So if you still want to help me, please feel free to do so.
The matter of 'where your script goes' is a rather general matter.
The script, is specific to one object, If the 'Player' is the only object that has that animation it will only be attached to the animation object.
However, if another object was to draw from that same principal, e.g. a 'enemy' or 'boostpowerspawn' was to 'boost up' you would apply the script to the enemy, (or the relevant object), but you would make sure the animation controller was linked up to that object .e.g.
anim = GameObjectFindWithTag("Enemy").GetComponent();
The link is important, it connects to the animation controller of the object
Let me know if you still have questions
*Also, from your question i'm not sure what 'boostpowerspawn' is, if you still need help, can you describe that for me a little bit better
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Animation spins wildly after completed 0 Answers
Reloading Help 2 Answers
Animation triggered by an animation? 1 Answer
cant play 3rd person animation 0 Answers