Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 this: 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? Everything 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 him to your animation, and on this transition, put a bool condition and select "true", also uncheck "has exit time";

  • Change this part of your script:

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

to

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

and this 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 things 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 think 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. $$anonymous$$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

Never$$anonymous$$d, 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.

Follow this Question

Answers Answers and Comments

263 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 avatar image

Related Questions

Animation constantly resetting? 0 Answers

my animation sprite is been block by the background 1 Answer

Animations not working 0 Answers

How to play the same animation for many objects(taken from the same prefab) but starting from different frames 1 Answer

2D animations transitioning too quickly and looping before finishing 2 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