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 mmorro1222 · Dec 06, 2016 at 08:45 PM · instantiatecolliderontriggerenterparentchild

OnTriggerEnter - check if collider is child

I'm making a 3d version of the classic game Asteroids. The player can fire torpedos at asteroids/enemy ships. When the player clicks the mouse button a torpedo is fired out from the player's ships location. The torpedo has a trigger collider on it which causes it to explode OnTriggerEnter. Torpedo is the child of the player ship. Currently, I have it working so that the torpedo checks if the collider that enters the trigger is the torpedo's parent, if not explode.

 void OnTriggerEnter(Collider other)
     {
         if (other != parentColl)
         {
             Instantiate(explosion, transform.position, transform.rotation);
             Destroy(this.gameObject);
         }
     }

what I need to be able to do is on the ship check if the collider entering the ships trigger is its child, to avoid damaging the player. That way, when the player's ship collides with an asteroid/enemy ship/enemy torpedo, they take damage. Currently, firing a torpedo causes damage to player without exploding the torpedo. How can I check if the collider passed into OnTriggerEnter is a child of the player ship?

instantiation of torpedo (in script attached to player ship):

 if (Input.GetButtonUp("Fire1"))
             {
                 Instantiate(torpedo, this.transform.position, this.transform.rotation, this.transform);
             }


Comment
Add comment · Show 3
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 mmorro1222 · Dec 06, 2016 at 06:15 PM 0
Share

Would there be a way to set the instantiated torpedo's tag to something like PlayerTorpedo within the script that instantiates it? That way I could check tag and go from there. I tried doing this by instantiating within a variable, but I can't alter the tag because the variable is apparently an object, rather than a gameObject. Which confuses me because I've seen similar things done elsewhere, but usually with java.

Though I suppose I could set up two torpedo prefabs with nothing different other than tag.

avatar image mmorro1222 mmorro1222 · Dec 06, 2016 at 06:32 PM 0
Share

Setting up to separate prefabs and checking if tag = PlayerTorpedo has worked, though i'd still like to know if there is a better/different way.

avatar image AlwaysSunny · Dec 06, 2016 at 08:48 PM 0
Share

Why is the torpedo a child of the object that spawned it? That doesn't sound right at all.

Unless I'm badly mistaken, child colliders shouldn't fire collision events with their parents in the first place. This is what allows people to build complex shapes like humanoid shapes with hitboxes on limbs, etc.

So there shouldn't be a problem right now - the problem you're describing should come into play when you make the torpedo independent of the player like you're (almost always) supposed to.

Anyway, when that happens, you'll want to look into different layers, or simply responding to the projectile's "who fired me" reference so that if it's YOU, you don't set off the explosion.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AurimasBlazulionis · Dec 06, 2016 at 08:56 PM

Assuming the ships are the root transforms you can check it using something like if (other.transform.root != parentRoot). You do not really need to check the colliders, but instead, transforms.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnTriggerEnter not being called with multiple colliders 1 Answer

How to Instantiate Prefab as Child of Player OnTriggerEnter 0 Answers

Make a simple tree 1 Answer

Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer

How to find and remove the last child of parent? 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