• 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
Question by GregXavier · May 02, 2013 at 11:34 PM · gameobjectcollidertriggermultiple

Child collider triggering parent collider effects.

What I want to happen:

I want my meteor to trigger a swooshing noise when it is close to an object it can hit.

How I set it up:

I have an object (a meteor) that damages other objects (for example, a spaceship) when it collides with them.

I have attached an empty game object to the meteor and given it it's own collider (trigger) with a wider sphere. This collider does trigger the swoosh.

My problem:

The wide 'swoosher' collider on the child game object still triggers the collision effects that are supposed to be triggered only if the meteor collider hits the spaceship.

Code:

Here is the script attached to the child gameobject that is nothing more than a wide collider. My 'swoosher'. This gameobject is a child of the meteor, whose collider triggers damage (meteor code below).

 public class scriptMeteorSwoosher : MonoBehaviour
 {
     //inspector variables
     public GameObject swoosh1;
     public GameObject swoosh2;
     public GameObject swoosh3;
     
     void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.tag == "starbase")
         {
             //play one of the three sound effects
             int rand = Random.Range(0,3);
             switch(rand)
             {
                 case 0: Instantiate(swoosh1, transform.position, transform.rotation); break;
                 case 1: Instantiate(swoosh2, transform.position, transform.rotation); break;
                 case 2: Instantiate(swoosh3, transform.position, transform.rotation); break;
             }
         }
     }
 }

Here is the code for my meteor.

 public class scriptMeteor : MonoBehaviour
 {
     //inspector variables
     public int spawnTimeMin                = 20;                //minimum seconds between meteor spawning
     public int spawnTimeMax                = 40;                //maximum seconds between meteor spawning
     public float speed                    = 0.1f;
     public ParticleSystem missileHit;
     public ParticleSystem baseHit;
     
     //private variables
     private float spawnTimer;                                    //for recording current spawn time
     private Vector3 direction;
 
     
     // Use this for initialization
     void Start ()
     {
         //generate an initial value for the spawntimer
         spawnTimer = Random.Range (spawnTimeMin,spawnTimeMax);
     }
     
     // Update is called once per frame
     void Update ()
     {
         MeteorSpawner();
         UpdateMeteor();
     }
     //function for moving the meteor to it's new location
     void MeteorSpawner()
     {
         //declare required variables
         float randx;
         float randy;
         float randDirX;
         float randDirY;
         Vector3 tempPos = new Vector3(0,0,0);
         //reduce the spawntimer by one per second
         spawnTimer = spawnTimer -(1 * Time.deltaTime);
         
         //when it is time to spawn the meteor again
         if(spawnTimer <= 0)
         {
             //generate new random x/y coords where maximums are outside viewable game window
             randx = Random.Range (-17.0f,17.0f);
             randy = Random.Range (-10.0f,10.0f);
             
             //make sure it spawns outside the viewable game are but not too far outside
             //width
             while((randx > -16.5) && (randx < 16.5))
             {
                 randx = Random.Range (-17.0f,17.0f);
             }
             //height
             while((randy > -9.5) && (randy < 9.5))
             {    
                 randy = Random.Range (-10.0f,10.0f);
             }
             //if it's above y zero, point it down
             if(randy > 0)
             {
                 randDirY = Random.Range (-180.0f,-1.0f);
             }
             //else point it up
             else
             {
                 randDirY = Random.Range (1.0f,180.0f);
             }
             //if it's left of x zero, point it right
             if(randx > 0)
             {
                 randDirX = Random.Range (-180.0f,-1.0f);
             }
             //else point it left
             else
             {
                 randDirX = Random.Range (1.0f,180.0f);
             }
             //combine the direction for the meteor to go into a new vector3 and normalize it
             direction = new Vector3(randDirX,randDirY,0);
             direction.Normalize();
             //assign tempPos the new vector3 and move the meteor there
             tempPos = new Vector3(randx,randy,2);
             transform.position = tempPos;
             //reset the spawntimer
             spawnTimer = Random.Range (spawnTimeMin,spawnTimeMax);
         }
     }
     
     void UpdateMeteor()
     {
         //just move the meteor forward. that is all
         transform.position += direction * speed;
     }
     //if it collides with anything it needs to do something about
     void OnTriggerEnter(Collider other)
     {
         //if it hits a missile pretend it's destroyed by moving it out of the viewable area
         if(other.gameObject.tag == "missile")
         {
             Instantiate(missileHit, transform.position, transform.rotation);
             Vector3 awayWithThee = new Vector3(0,0,500);
             transform.position = awayWithThee;
         }
         
         //if it hits a starbase, change it's Z position so it will not trigger any more collisions until it is moved again
         if(other.gameObject.tag == "starbase")
         {
             Instantiate(baseHit, transform.position, transform.rotation);
             transform.position = new Vector3(transform.position.x, transform.position.y, 5.9f);
         }
     }
 }
Comment
youblistermypaint
SaturnoMichael
SpinyPirate
chemariz
Prime624
ojaoweir

People who like this

6 Show 0
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
Best Answer

Answer by GregXavier · May 02, 2013 at 11:42 PM

Derp. Found the answer here... Evidently my initial google was not sufficient!

http://answers.unity3d.com/questions/410711/trigger-in-child-object-calls-ontriggerenter-in-pa.html

Works flawlessly when I give the swoosher collider a kinematic rigidbody.

Comment
youblistermypaint
mos3465
luislodosm
SpinyPirate
Orrib
daviddickball
dyenaldinesh
hery41
chemariz
Prime624
EduardoBastos
Paul-Bones
JKrypto
unity_9C3ACB7E07A327CC6514

People who like this

14 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 SpinyPirate · Apr 19, 2017 at 09:52 AM 2
Share

Thank you! It works. You comment helped me SO much!

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

13 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

Related Questions

Trigger can't see the enemy 2 Answers

Multiple Colliders Trigger problem 2 Answers

Can't click gameobject when over another trigger? 1 Answer

GameObject going into terrain. 0 Answers

I need help with triggers 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