• 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 Aspect13 · Dec 24, 2018 at 08:35 PM · animationanimatoranimationsnot workinganimate

Animator not playing onCollisionEnter.

Hey, I want ammo box to open and close when Player collides with it. My code looks like t$$anonymous$$s: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class AmmoBoxAnimator : MonoBehaviour
 {
     Animator animator;
 
     private void Start()
     {
         animator = GetComponent<Animator>();
     }
 
     private void OnCollisionEnter(Collision col)
     {
         if(col.transform.tag == "Player")
         {
             animator.Play("opened_closed");
         }
     }
 }

I attached my script to every ammo box I have and checked the name of animation: alt text alt text

Why isn't it working? Everyt$$anonymous$$ng is fine I guess...

screenshot-1.png (58.2 kB)
screenshot-2.png (48.4 kB)
Comment
Add comment
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

3 Replies

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

Answer by Otavio_ · Dec 26, 2018 at 11:16 PM

  • Create another state in animation, empty, set as default;

  • Make a transition from $$anonymous$$m to your animation, and on t$$anonymous$$s transition, put a bool condition and select "true", also uncheck "has exit time";

  • Change t$$anonymous$$s part of your script:

     if(col.transform.tag == "Player")
          {
              animator.Play("opened_closed");
          }
    
    

to

      if(col.transform.tag == "Player")
      {
          animator.SetBool("nameofyourcondition", true);
      }

and t$$anonymous$$s should work...

Hint: i suggest to you to change the "collider checker" from every bullet to only the player. So, you will have less scripts checking every time if someone collide*

Comment
Add comment · Show 6 · 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 Aspect13 · Dec 27, 2018 at 05:12 PM 0
Share

I've never used animations. The animation is from asset store. I will try this later though, and see if it works.

avatar image Aspect13 · Dec 28, 2018 at 06:07 PM 0
Share

So I did what you told me, I guess I did it correctly :/ It's still not working though.

Here are screenshots links because for some reason I can't upload them from my PC: https://imgur.com/a/mc7Hwrh

avatar image Otavio_ Aspect13 · Dec 28, 2018 at 07:15 PM 0
Share

you forget something: you have to create a parameter in animator > parameters > + > bool > set the name as you choose (playAnimation) then you go in the arrow of transition and re-choose the parameter that you just created

avatar image Aspect13 Otavio_ · Dec 28, 2018 at 07:45 PM 0
Share

Okay, I've created a paramater "playAnimation" as you said should I set it to true or false in the transition? I've set it to false and It's not working.

Edit: true is working but only once and I want to it do the animation every time I collide with an object. How to do it?

Show more comments
avatar image
0

Answer by Aspect13 · Dec 26, 2018 at 08:38 PM

Please someone help? Should I set the Animator to public at attach the animation to it?

Comment
Add comment · 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
0

Answer by RustyCrow · Dec 29, 2018 at 06:19 PM

You need to google unity animations my dude, i see that you have some basic understanding but you are missing the other half and the best way is to just look at some one else do it. But from what i see is you are missing 3 t$$anonymous$$ngs Parameter A transition and a State(empty) (yours is default so it will happen on play) and Code to trigger (take a look at the Setbool() etc funcs). Going through every step needed i dont t$$anonymous$$nk anyone is willing to write down.

Comment
Add comment · Show 2 · 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 Aspect13 · Dec 29, 2018 at 08:48 PM 0
Share

Thanks I did some research about what you said and it seems to be working the only problem is it plays only one time. E.g I collide with first ammo box it play animation, then I go to the second ammo box on the map and it plays animation etc. but when I go the first ammo box once again it does not play animation any more.

avatar image Aspect13 · Dec 29, 2018 at 09:20 PM 0
Share

Nevermind, fixed it! Thanks!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

262 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 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 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 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 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 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 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 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 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 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 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 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

Animation constantly resetting? 0 Answers

my animation sprite is been block by the background 1 Answer

Dynamically add AnimationState to Controller 0 Answers

Animations will play for a second perfectly then stop HELP 1 Answer

Best way to queue animations with Animator 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