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 /
  • 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, which is a cube with a Box Collider (Is Trigger checked).

This 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 which does nothing 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 nothing updating the transform.

Now here is the strange thing, with this 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 nothing 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 something wrong? Does causing the transform to refresh by translating it activate the rigidbody or something?

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

is the debug on the bullet or the target?

avatar image Phraides · Oct 06, 2011 at 01:44 PM 0
Share

The debug message is on the bullet which has a trigger Box Collider.

avatar image Kacer · Oct 06, 2011 at 01:47 PM 0
Share

hmm, it does indeed sound odd, i would think it is because your trigger is stationary, but i've got stationary triggers in a few of the games i've made as well, and i dont have this issue.

could you try going into edit -> project settings -> physics and then set sleep and sleep angular velocity to 0, see if that changes anything.

Tell me if that changes anything.

edit: doing that will make physics update everything every frame, even if its stationary, its a resource hog.

also, what happens if you have the debug on the trigger?

avatar image Phraides · Oct 06, 2011 at 01:55 PM 0
Share

I am not sure of the terms you use, so just to be clear:

Bullet == moving object, box collider with trigger (this object should receive the trigger event with the debug message), no rigidbody

Target == stationary object, box collider without trigger, has a rigidbody

Now if I add a rigidbody on the bullet, it works.

Concerning the sleep velocities, it's still the same even if I set them to 0

avatar image Kacer · Oct 06, 2011 at 01:58 PM 0
Share

wait, it works with a rigidbody now? >_>

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;

This 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 things are not moving they cannot hit each other.

I hope this 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

This is HUG$$anonymous$$ I wish it was better documented, because I just realized it's the source of almost all trigger problem's I've had in unity...

avatar image JCCCoelho · Oct 07, 2013 at 03:11 PM 0
Share

This... helped a lot.

I tried to forcefully wake up the rigid body every frame but it didn't work.

Adding rigid bodies to every object you want to pick up works, but performance takes a hit.

Awesome Solution ;D

avatar image Brockoala · Nov 03, 2016 at 07:57 AM 0
Share

Thanks for the trick! I just tried with Unity 5.35, a + Vector3.zero didn't work in this version, but + Vector3.right x 0.001f worked (and - Vector3.right x 0.001f in the next frame to ensure consistency).

avatar image noamraz · Apr 20, 2020 at 01:41 AM 0
Share

thank you so much!!

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)" thing 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

I just tried but I have the same result as using OnTriggerEnter...

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.

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

OnTriggerEnter no longer works 1 Answer

How to Make A Character Stop At Wall? 0 Answers

Rigidbody2D figits when pushed against an object. 0 Answers

Rigidbody randomly going through collider 1 Answer

Freezed object loses it's collider 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