Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 NizarA · Jul 14, 2014 at 03:39 PM · collider 2d

OnTriggerEnter2D working with non-trigger collider

Hi,
I am actually working in an action/platformer.
I prototyped a falling platform, but it didn't work as expected. My platform is composed of a single game object, with two colliders: the first one is used to act like a proper collider, and the second one, a trigger collider, is a small box on top of the "normal" collider. I did that so the platform did not self-destroy if the player hit it from behind.

alt text

I have also a script attach to it, which will launch a Coroutine if the function OnTriggerEnter2D is called.

     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player")
         {
             //Animation
             StartCoroutine(DestroyAndReconstruct());
         }
     }

My player's collider is a trigger. After some debug, it seems that the function OnTriggerEnter2D is called when the player hits the non-trigger collider. It happens even when I remove all trigger from my platform.

Did I do anything wrong?

collider.png (8.1 kB)
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

2 Replies

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

Answer by Novemb3r · Jul 14, 2014 at 06:11 PM

When you have a trigger on either object that is colliding, OnTriggerEnter functions from any script that is attached to EITHER object get called. So since your player is a trigger, when the player makes contact with the collider you call OnTriggerEnter on both your player's scripts AND the scripts on your platform.

Your new problem would then be figuring out how to detect which side of the box you collided on so that you can execute your code only when it contacts from whatever side you want it to.

There is the simple option where you make two objects connected into one GameObject with different colliders and only one of those objects has the coroutine inducing script (which would need to be edited to function on both objects).

Or probably a better solution would be to look into raycasting and using that to determine the sides your character hit from.

This is a link to another question that explains how to use raycasting for the solution:

How to Detect Which Side of a Box Was Collided With

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 NizarA · Jul 15, 2014 at 07:36 AM 0
Share

Thank you, that was what I was looking for!

avatar image
0

Answer by alwayscodeangry · Jul 14, 2014 at 05:44 PM

See this answer for details, but basically you can't have two colliders on the same object act differently.

Try making a child object with the trigger and associated script.

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 NizarA · Jul 15, 2014 at 07:36 AM 0
Share

Thank you for the link!

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

2D Physics and Jump-Through Platforms 2 Answers

Unity colliders bug (2d box) 2 Answers

projectiles not coliding with character 3 Answers

Pixel Perfect 2D Polygon Collider 2 Answers

Raycast 2D not working as i hoped 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