• 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 nictm · Jul 13, 2020 at 02:29 PM · collision2dtriggering animation box collider

How to trigger an animation based on the collision of two other gameObjects

Hello,

Here is my scenario, I have three boxes. Box 1 has the script and the animation. Is there a way to trigger the animation in Box 1, based on the collision of Box 2 and Box 3. One note: when Box 2 and Box 3 collides, Box 2 is destroyed.

Just for more specifics if needed: I have an enemy (box 1), shooting a projectile (box 2), at the player (box 3). Upon the projectile hitting the player, the projectile is destroyed and then I want the enemy to jump up and down excited that he hit the player.

I just need help with the triggering of the animation in this scenario.

I have tried a bool based on OnCollisionEnter2D and OnTriggerEnter2D with the projectile and the player, but no matter what I try (which has been many different ways), even though the bool turns true on collision, I can get that bool to register true on the enemy. So I'm guessing this is the wrong way to go about doing this. The only other way I can think of this working is my scenario above, in having the animation trigger based on the collision of two objects (neither of which the script or animation is on).

Any help with this would be great and if you need any other information, just let me know.

Thanks, Nic

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

1 Reply

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

Answer by unity_ek98vnTRplGj8Q · Jul 13, 2020 at 10:10 PM

The simplest way to do this would be something like this

 //Put this function in the script attached to your enemy unit
 public void TriggerAnimation(){
     //Triggers the animation of the enemy
 }
 
 //When you instantiate a bullet, give the script below a reference to the enemy object
 Gameobject newBullet = Instantiate(blah blah blah)
 newBullet.GetComponent<BulletCollisionDetection>().sourceEnemy = this;
 
 ////////////////////
 
 //Attach this script to your bullet prefab
 public class BulletCollisionDetection : Monobehavior{
     //This will hold a reference to the script that is on your enemy (I have assumed the script is called 'EnemyScript')
     public EnemyScript sourceEnemy;
 
     public void OnCollisionEnter2D(Collision2D col){
         //Somehow check that you are hitting the player
         if(col.tag == "Player"){
             sourceEnemy.TriggerAnimation();
         }
     }
 }
Comment
Add comment · Show 1 · 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 nictm · Jul 17, 2020 at 01:32 PM 0
Share

Thanks! @unity_ek98vnTRplGj8Q This worked!

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

131 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

Related Questions

OnTriggerEnter2D often not called 1 Answer

Animation triggers are not firing 0 Answers

[2D] Rigidbody trips when walking 4 Answers

detecting collision of collided object 2d 1 Answer

Having a problem with damage over time on unity 2d 1 Answer


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