• 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 /
  • Help Room /
avatar image
5
Question by Phraides · Oct 06, 2011 at 01:12 PM · collisionrigidbodycollidertransformontriggerenter

OnTriggerEnter doesn't work if the colliding object is not moving. Translating it by (0,0,0) fixes it. What's the problem?

I have a problem getting a OnTriggerEnter event using a moving object colliding with a non moving object. Let me explain:

I have two objects in the scene. The first one is my bullet, w$$anonymous$$ch is a cube with a Box Collider (Is Trigger checked).

T$$anonymous$$s bullet is moving towards a target with the simple following script:

 void Update () {
     transform.Translate(0, 1*Time.deltaTime, 0);
 }

It has an overriden OnTriggerEnter w$$anonymous$$ch does not$$anonymous$$ng except outputing a log message:

 void OnTriggerEnter(Collider otherObject){
     Debug.Log("Collided!");    
 }

The target is also a cube, with a Box Collider and a RigidBody (Is Kinematic checked). It is not moving, so there is not$$anonymous$$ng updating the transform.

Now here is the strange t$$anonymous$$ng, with t$$anonymous$$s setup only, the bullet does not trigger the event when it collides with the target. However, if I add a script to my target doing not$$anonymous$$ng but translating its transform with a null vector, as follows:

 void Update () {
     transform.Translate(0, 0, 0);
 }

it works and I do get the log message saying "Collided".

Does anybody know what the problem is? Am I doing somet$$anonymous$$ng wrong? Does causing the transform to refresh by translating it activate the rigidbody or somet$$anonymous$$ng?

Edit: it also works without the null translate if I add a RigidBody (IsKinetic checked) on the Bullet.

Comment
Add comment · Show 6
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 Kacer · Oct 06, 2011 at 01:39 PM 0
Share
avatar image Phraides · Oct 06, 2011 at 01:44 PM 0
Share
avatar image Kacer · Oct 06, 2011 at 01:47 PM 0
Share
avatar image Phraides · Oct 06, 2011 at 01:55 PM 0
Share
avatar image Kacer · Oct 06, 2011 at 01:58 PM 0
Share
Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Kid Canuck · Apr 22, 2013 at 01:11 AM

I'm having a similar issue. I am spawning an object with a sphere collider trigger in front of my character (capsule collider, rigidbody). The spawned object does a bit of its own setup (when it is spawned, it will fall until it is on the ground, then set the radius of the collider). If I am not moving, and I spawn the object, I do not get any OnCollider messages at all (even though the capsule is completely inside the sphere). If I move my character at all, I get the OnTriggerEnter message and it works fine.

It seems to me that if neither object is moving*, it will not even check for trigger / collider interactions.

My fix was, when I turned on the collider and set the size for it, I waited 1 frame and then 'nudged' the object myTransform.position = myTransform.position + Vector3.zero;

T$$anonymous$$s forces the physics system to respond, and even though it did not actually move, it processes the colliders and notices the interaction. Seems likely to me it's an optimization based on the pretense that if 2 t$$anonymous$$ngs are not moving they cannot $$anonymous$$t each other.

I hope t$$anonymous$$s helps others with similar issues.

Comment
Add comment · Show 4 · 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 hephaestus_sc2 · Jun 04, 2013 at 05:59 AM 0
Share
avatar image JCCCoelho · Oct 07, 2013 at 03:11 PM 0
Share
avatar image Brockoala · Nov 03, 2016 at 07:57 AM 0
Share
avatar image noamraz · Apr 20, 2020 at 01:41 AM 0
Share
avatar image
2

Answer by OddSlice1 · Jun 24, 2017 at 05:12 AM

I had a similar problem, I spawned in an object and wanted to know if it was colliding with another. Both are kinematic RigidBodies 2D with 2DColliders and the spawned in object wouldn't call its OnTriggerEnter2D() function. The way I solved it was by changing the Rigidbodies 2D sleep options to "Never Sleep". I believe that in the default state, "Start Asleep" an object "Wakes up" and starts checking for collisions only when it starts to move, thus the "translate (0,0,0)" t$$anonymous$$ng working.

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
avatar image
0

Answer by IMTRIGGERHAPPY9 · Oct 06, 2011 at 02:19 PM

Try using OnTriggerStay that should work perfetly

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 Phraides · Oct 06, 2011 at 03:53 PM 0
Share
avatar image
0

Answer by JimRustler · Feb 06, 2014 at 01:11 AM

I believe the problem is with the way that Unity checks for collisions. It doesnt make sense to check every single object for collisions constantly so they most likely only check the objects that have moved that frame. Thats why translating by (0,0,0) works because it marks it as "moved" for that frame.

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.

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

12 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

Related Questions

How to Make A Character Stop At Wall? 0 Answers

OnTriggerEnter no longer works 1 Answer

Collision between objects not being detected 0 Answers

Road problem. Detect collision without effectively colliding with a rigidBody 1 Answer

Colliders "offset" and going down too. 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