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 MergenGames · Sep 08, 2016 at 11:31 AM · rigidbody2dcollider2dontriggerenter2d

OnTriggerEnter2D - am I getting it wrong?

I have 2 game objects, lets say them A and B

A has a 2D box collider and isTrigger is selected and this A object is sign static. B has a 2D box collider too and its isTrigger also activated. B also has a kinematic rigidbody2D.

When these two game objects collide with each other OnTriggerEnter2D(Collider2D other) function is only fired on object A not on object B. It is also not fired when two B game objects collides. What am I doing wrong? Any help is appreciated.

Thank you people.

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 Firedan1176 · Sep 08, 2016 at 11:41 AM -1
Share

OnTriggerEnter fires when one object has a rigid body and both have colliders. It also only fires on the object that is "intruded". In the case of the 2 object 'B's, at least one if them ha's to be NOT is$$anonymous$$inematic.

Hope this helps

avatar image tanoshimi Firedan1176 · Sep 08, 2016 at 01:17 PM 0
Share
  • At least one object has a rigidbody.

  • The message is sent to both the trigger Collider and the Rigidbody (if any) that the trigger Collider belongs to, and to the Rigidbody (or the Collider if there is no Rigidbody) that touches the trigger

  • The rigidbody does not need to be non-kinematic to detect a trigger

avatar image tanoshimi · Sep 08, 2016 at 01:14 PM 0
Share

That's not right... can you post a screenshot showing the component setup on each object, and the scripts you're using to detect the OnTriggerEnter? Are you sure you're not collapsing messages in the Log console?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MergenGames · Sep 09, 2016 at 02:18 PM

Hey Guys, sorry for late response, I really appreciate your help, so first of all thank you.

Below I add some images and code blocks as @tanoshimi requested.

One of my objects which is moving across screen:

alt text

The collectible standing still object:

alt text

here is my player code block for ontrigger:

 void OnTriggerEnter2D(Collider2D other)
     {
         Debug.Log("hoorey");
     }

and collectible ontrigger:

 void OnTriggerEnter2D(Collider2D other)
     {
         GameObject head = other.GetComponentInParent<PlayerController>().head;
         GameObject tail = other.GetComponentInParent<PlayerController>().tail;
         head.transform.localScale = new Vector2(head.transform.localScale.x * 1.5f, head.transform.localScale.y * 1.5f);
         head.transform.localPosition = new Vector2(0,0);
         tail.transform.localScale = new Vector2(tail.transform.localScale.x * 1.5f, tail.transform.localScale.y * 1.5f);
         tail.transform.localPosition = new Vector2(0, tail.transform.localPosition.y +  tail.transform.localPosition.y/2);
         
         other.GetComponentInParent<PlayerController>().UpdateHealth(10.0f);
         Destroy(gameObject);
     }





player.jpg (34.9 kB)
collectible.jpg (18.2 kB)
Comment
Add comment · Show 5 · 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 tanoshimi · Sep 09, 2016 at 02:32 PM 0
Share

your second object has no Rigidbody, so no trigger will fire if two collide. Which object also has the PlayerController attached?

avatar image MergenGames tanoshimi · Sep 10, 2016 at 07:07 AM 0
Share

PlayerController is Attached to object with rigidbody? There is trigger fired on the object which has only collider. $$anonymous$$y problem is trigger is not fired on the object with rigidbody. And when same rigidbody objects that I mention in my question collides, there is no trigger fired between them.

avatar image tanoshimi MergenGames · Sep 10, 2016 at 07:53 AM 0
Share

You said your problem is "It is also not fired when two B game objects collides". That's because neither have a rigidbody attached. (I'm assu$$anonymous$$g your screenshots show ObjectA and then ObjectB?)

If there is no PlayerController attached to tbe other object, then this line will make head null: GameObject head = other.GetComponentInParent().head;

In which case, I'd expect this to generate a null reference exception: head.transform.localPosition = new Vector2(0,0);

Are you sure there's nothing in the Log console?

Show more comments
avatar image
0

Answer by m0guz · Sep 10, 2016 at 07:43 AM

I read your posts, other possibility is Layer-based collision detection

First check object's layer in Inspector window, then Go to Edit > Project Settings > Physics2D and check if layer is selected.

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 MergenGames · Sep 10, 2016 at 09:07 AM 0
Share

Thank you for spending your time on my problem m8, really appreciate it.

In my case, at the end all layers can collide with each other. So I thought it is not necessary. Am I wrong about this idea?

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

OnTriggerEnter2D fires multiple times when Animator component is on 3 Answers

Static Colliders in the 2D Engine in Unity 5 1 Answer

Weird behaviour after Collision? 0 Answers

Using child colliders with rigidbodies/joints in 2D 0 Answers

2D Rubber hands (Weird question) 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