Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dogg · Jun 18, 2014 at 11:03 PM · animationpowerup

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.

alt text

alt text

animator.png (35.3 kB)
characteranim.png (10.4 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dogg · Jun 21, 2014 at 11:40 PM 0
Share

(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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dogg · Jun 19, 2014 at 01:48 AM 0
Share

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?

avatar image RyanPaterson · Jun 19, 2014 at 12:52 PM 0
Share

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

avatar image Dogg · Jun 19, 2014 at 10:24 PM 0
Share

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:

alt text

alt text

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.

inspectortransition.png (21.3 kB)
animatorboost.png (35.5 kB)
avatar image Dogg · Jun 21, 2014 at 09:42 PM 0
Share

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.

avatar image RyanPaterson · Jun 22, 2014 at 01:19 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges