Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 personWithQuestion · Dec 27, 2011 at 10:17 PM · collisionrigidbodyontriggerenterparenting

Child calling parent's OnTriggerEnter when no Rigidbody is attached to child

I have a parent child relationship with a Player (the parent) and a Shield (the child). This was done so that when I instantiate the shield, it follows the Player as he moves.

The Player has a Kinematic Rigidbody attached, and the Asteroids that can collide with the Player or the Shield also have Kinematic Rigidbodies attached. The Shield does not have a Rigidbody attached. All of the above are marked in their Collider components as 'Is Trigger'.

In the above scenario, if a Player turns on their shield and an Asteroid hits it, the Player's OnTriggerEnter function is called, rather than the Shield's. Visually, the Asteroid is clearly being destroyed on contact with the Shield.

The desired behavior is for the Shield's OnTriggerEnter function to be called when it collides with an Asteroid, not the Player's. This happens in two scenarios. The first is if the Shield has a Rigidbody attached. The second is if a parent-child relationship isn't created between the Shield and the Player.

From reading this http://unity3d.com/support/documentation/Components/class-MeshCollider.html it seems like I have a Static Trigger Collider (the Shield) colliding with a Kinematic Rigidbody Trigger Collider (the Asteroid) which should send a message (and it is). The problem is that the message is being sent to the parent (the Player), rather than the Shield. I find this odd, since simply attaching a RigidBody to the Shield solves the problem.

I've pasted the code that establishes the parent-child relationship, and the OnTriggerEvent functions. Any help is appreciated.

     // Player activates shield when he presses E.
     if (Input.GetKeyDown(KeyCode.E) && !shieldActivated)
     {
         shieldActivated = true;
         Transform shieldTransform = Instantiate(shield, transform.position, transform.rotation) as Transform;
         shieldTransform.parent = transform;
         Shield shieldScript = shieldTransform.GetComponent<Shield>();
         shieldScript.Deactivated += new EventHandler(DeactivateShield);
     }


 // Player's OnTriggerEnter
 void OnTriggerEnter(Collider other)
 {
     if (System.String.Compare(other.tag, "Asteroid") == 0)
     {
         Lives -= 1;
         if (Crashed != null)
         {
             Crashed(this, EventArgs.Empty);
         }
     }
 }
 

 // Shield's OnTriggerEnter
 void OnTriggerEnter(Collider other)
 {
     if (other.tag.Equals("Asteroid"))
     {
         hitsLeft -= 1;
         if (hitsLeft <= 0)
         {
             if (Deactivated != null)
             {
                 Deactivated(this, EventArgs.Empty);
             }

             Destroy(gameObject);
         }
     }
 }

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 personWithQuestion · Dec 28, 2011 at 03:45 PM

Finally found this thread which answered my question. Apparently this is just how the engine works. I ended up removing the Rigidbody from the Player since it wasn't necessary. http://forum.unity3d.com/threads/36462-onCollisionEnter-does-not-work-on-a-child-colider

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I stop a timer with OnTriggerEnter()? 0 Answers

How to make a collectible item collide with terrain but not player? 4 Answers

Rigidbody parenting collisions question (katamari effect). 1 Answer

Player colliding with pickable object 0 Answers

Waypoint Question(Conflicting Collision) 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