• 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 Utoxin · May 31, 2016 at 06:50 PM · scripting problemcolliders

Event Triggering On Wrong (Parent) GameObject

I have a construct in my game that consists of a group of GameObjects like this:

 Ship (Convex Mesh Collider / Rigid Body)
     Shield Parent
         Shield Emitter Element (Box Colliders)
         ... (Multiple Emitters)

The Ship and the Shield Emitter Elements both make use of a script component that is designed to handle collisions and track damage to the GameObject they are attached to.

The problem is that when something collides with the ShieldEmitterElement, the collision event gets called on Ship instead and I have no idea why. I've checked the bounds of the colliders for both items, and the Ship collider is definitely NOT extending out past the Shield Emitter Element, but it is definitively calling the script on the Ship, and the damage is being applied there.

This is the method in question:

     public virtual void OnCollisionEnter(Collision collision) {
         if (collision.gameObject.tag == "Dust") {
             return;
         }
 
         DamagePacket Damage = new DamagePacket();
 
         if (collision.gameObject.tag == "WeaponProjectile") {
             DamageSource projectile = collision.gameObject.GetComponent<DamageSource>();
 
             if (projectile.OriginShip == gameObject) {
                 return;
             }
 
             Damage = projectile.CalculateDamage();
 
             Instantiate(projectile.impactParticles, collision.transform.position, Quaternion.Inverse(collision.transform.rotation));
             Destroy(collision.gameObject);
         } else {
             if (collision.gameObject.GetComponent<Destructable>() != null) {
                 //Rigidbody otherRB = collision.rigidbody;
                 Damage = collision.gameObject.GetComponent<Destructable>().CollisionDamage(RigidBody.mass, collision.relativeVelocity.magnitude);
             }
         }
 
         TakeDamage(Damage);
     }
 

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 Utoxin · May 31, 2016 at 07:00 PM 0
Share

I just stumbled over the concept of Compound Colliders, and it sounds like this may be what's happening to me. Do I need to re-structure my object collection so that the Ship's rigid body isn't a parent of the Shield Emitter colliders?

2 Replies

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

Answer by Alec-Slayden · May 31, 2016 at 07:58 PM

You're most likely running into compound colliders, as you stated, if your parent object has a rigidbody. There are a couple ways to resolve it, including careful use of kinematic rigidbodies for individual child colliders, but in your case the most straightforward solution would be to use collision.collider instead of collision.gameObject. unlike the game object and transform references, which return the parent rigidbody object or transform, the collider property returns the immediate collider hit. You can use this to navigate your components in the same way you are using gameObject currently.

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 Utoxin · May 31, 2016 at 08:02 PM 0
Share

Ahha! Thanks for that. I'm already most of the way through re-structuring the object tree, so I'll see if I have any issues with that once it's done, and I may revert to this structure if I do, and just use your suggestion. I'll certainly keep it in $$anonymous$$d for the future as well.

avatar image
0

Answer by wojtask12 · Jun 01, 2016 at 01:35 PM

I've had the same issue couple days ago - had some Spikesas children of rigidbody. This script attached to parent worked. Just modify it to your needs

 using UnityEngine;
 using System.Collections;
 
 public class ObstacleCollisionDetector : MonoBehaviour {
 
     void OnCollisionEnter(Collision coll)
     {
         foreach (var contact in coll.contacts)
         {
             if (contact.thisCollider.GetComponent<Spike>() != null && contact.otherCollider.CompareTag("Player"))
                 EventsManager.Instance.InvokeCrashEvent();
 
         }
     }
 }
 

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.

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

52 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

Related Questions

How to move Groups of Objects using a Script? 0 Answers

How can i make the child collider turn off and on instead of the parent collider? 1 Answer

How to Pick up 1 collider Object at a time, Vs. all at once? Item Script 1 Answer

Character Controller squeezed between colliders freezes Unity 0 Answers

Root motion or script motion to make a character like riven? and any suggestions for learning resources 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