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 rct3fan24 · Nov 11, 2014 at 08:36 PM · triggerplatform

How can I call OnTriggerEnter2D from the object with the trigger on it?

So uh, I'm trying to make a one way platform, and it's proving to be difficult.

I'm trying to use a trigger object that goes underneath the platform, and when the player enters the trigger, it does the Physics2D.IgnoreCollision thing between the player's collider and the platform's collider.

The problem I'm having is that I'm trying to call OnTriggerEnter2D/Exit2D from a script on the trigger object, and I don't think that's working. Is there some solution to this?

Edit: I should have elaborated more. I need to figure out how to detect if the player has entered the trigger from the trigger object itself, not the player. Is that possible?

Comment
Add comment · Show 2
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 Xysch · Nov 11, 2014 at 09:10 PM 0
Share

Did you select trigger on the collider? I seem to forget that once in a while.

avatar image rct3fan24 · Nov 11, 2014 at 10:56 PM 0
Share

@Xysch, yes, Is Trigger is checked.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by spiceboy9994 · Nov 11, 2014 at 09:14 PM

In order to detect collisions, at least one of the elements colliding should have a RigidBody, in this case a RigidBody2D, usually you add this to your moving element (the player) as best practice. Does your player have this component attached?

Hope this helps

Comment
Add comment · Show 3 · 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 rct3fan24 · Nov 11, 2014 at 10:58 PM 0
Share

Yes, the player has a RigidBody2D. That's not the problem. I need to figure out how to detect if the player has entered the trigger from the trigger object itself, not the player. Is that possible?

avatar image spiceboy9994 · Nov 13, 2014 at 06:50 PM 0
Share

Just curious, why do you need to detect the is trigger enter from the other object?, shouldn't be easier to handle that to the player and write something like this:

 void OnTriggerEnter2D(Collider2D other)
 {
     /*All the player triggers here*/
     /*my desired object trigger enter detection*/
     if (other.gameObject.tag == "$$anonymous$$yOtherObjectSpecificTag")
     {
       /*Execute functions*/
     }
 }

If there's an scenario where you need to execute other function on the external object, you can always use GetComponent, so it should look like this

     void OnTriggerEnter2D(Collider2D other)
     {
         /*All the player triggers here*/
         /*my desired object trigger enter detection*/
         if (other.gameObject.tag == "$$anonymous$$yOtherObjectSpecificTag")
         {
           /*Execute functions*/
           /*Find External component*/
           var externalComponent = other.gameObject.GetComponent<$$anonymous$$yExternalComponentType>();
           /*Execute external component functions*/
           externalComponent.publicFunction();
         }
     }


Just let me know if that helps

avatar image Bunny83 · Nov 13, 2014 at 07:13 PM 0
Share

@spiceboy9994: Regarding your suggestion in your comment: No it's not easier to handle such things in the player. That's not the responsibility of the player. If you would go by this logic you would have to put everything in the player when the player can interact with it.

It's usually easier to have a small script on the object itself. The purpose of an object should be reflected in a script on that object. For example coins that can be picked up would have a script that will check if the player collides with it (using OnTriggerEnter(2D)).

avatar image
0

Answer by Bunny83 · Nov 13, 2014 at 07:05 PM

Trigger messages are always sent to both objects involved. If your platform trigger is a static trigger collider it will receive an OnTriggerEnter2D event when the player objects enters the trigger.

However, If your platform collider is a child of another rigidbody object, the rigidbody will receive the event and not the child collider. This is because a rigidbody will "collect" all colliders which are a child of itself and use it as compound collider.

You would need to attach a kinematic rigidbody to your trigger object if you want to receive the message there in that case.

You might want to take a look at:

http://docs.unity3d.com/Manual/CollidersOverview.html

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

28 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

Related Questions

Platform Control 1 Answer

How to use rigidbody.MovePosition within OnTrigger? 0 Answers

How do I switch control values? 1 Answer

OnTriggerExit() help 0 Answers

Can't click gameobject when over another trigger? 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