- Home /
GameObject with 1 top Collider IsTrigger and 1 bottom regular Collider problem.
Please have a look at the picture. What I am trying to do is to allow Obj A to be able to fall down on Obj B which is constantly moving and stay on it. Obj A also can crush other Obj B above it by jumping towards them.
I have added 1 polygon collider 2D with IsTrigger to cover the top part of Obj A and then added a box collider 2D to the bottom/feet of Obj A. When Obj A jump and hit an Obj B above OnTriggerEnter2D will trigger which is correct. However, when Obj A fall down and touch an Obj B below both OnTriggerEnter2D and OnCollisionEnter2D will trigger which is not what I expect. I expect that box collider 2D will touch Obj B and trigger OnCollisionEnter2D only.
Why it has such behaviour and how could I solve this problem?
Answer by JonPQ · Jul 17, 2019 at 03:49 PM
well one solution would be to get rid of the trigger, and just use the collider and the direction and speed of A's Y movement.... up or down to control what to do when you hit something. e.g. if moving up and speed > smashThresholdSpeed, then smash B you could also then have a slower threshold to just jiggle B when its hit.... more flexible...
you could also try making the trigger on top of A very thin inY axis... and the box collider large... in case object A is moving down fast, say distance dy per frame... and dy> thickness of the current boxcollider... so when object moves down... it moves far enough in one frame, for both the collider and trigger to enter B.
Thanks for the advice.
The use of trigger is to prevent physics on Obj A when it jump and hits Obj B. If I don't use trigger then Obj A will act like jumping into a brick wall and fall down immediately but what I need is Obj A will go through Obj B which will transit into a blowing up animation.
I need Obj A's entire upper body to be able to collide with other objects too like A can run through a power up item so a very thin collider will not work for that purpose.
if you cant change the sizes of the colliders, check the collision type... if its set to discrete... (one check per frame of movement, its still possible both your colliders move far enough in 1 frame to be triggerred) then try CollisionDetection$$anonymous$$ode.ContinuousDynamic. If the problem is caused by moving in dy more than the thickness of your collider per frame... then doing this might help. see... Rigidbody.collisionDetection$$anonymous$$ode https://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetection$$anonymous$$ode.html
Just tried changing collision detection to ContinuousDynamic and still same problem. In fact collision gets trigger earlier than discrete mode.
Anyway, I have found another way to prevent this. In OnTriggerEnter2D I added a check of Obj A's rigidbody2d.velocity.y if it's negative means it is falling down so just exit the function while if it's positive it must be a jump so transit to blowing up animation.