- Home /
Collision happens, code executes but not working all the time
Recording of problem (the green brick) https://www.youtube.com/watch?v=0ftSmXsN-4A&feature=youtu.be
I have (RigidBody2D) ball hitting (RigidBody2D) bricks. Both have Collider2D on them. Bricks are instantiated from prefab at start, all have same "destroy" script that's hiding them and turning colliders off since at some point I want to re-enable them again.
When ball hits them, they act as they should: they hide and keep moving. However, sometimes (more like 1 in 20 hits) brick just fails to hide. It does collide but it starts moving as rigid object instead. I tried setting it on kinematic on hit, same issue.
What I tried:
Both bricks and ball collision detection on continuous and descrete
Both bricks and ball Interpolate to interpolate / extrapolate / none
Bricks move with Translate() / RigidBody.AddForce() / basic transform to side
Tried to actually Object.destroy() bricks on collision, same thing happens
Not sure what scripts I need to put here, so basically:
Bricks move:
transform.Translate(Vector3.right * speed * Time.deltaTime);
Collision detection on bricks:
edit: for some reason collision script broke and is showing some weird symbols. I'm not home to repost it till tomorrow.
Even on times when brick is not destroyed but actually bounced back, all Debug.Log() calls show right logs: the same brick is false to renderer, false to collider.enabled and isKinematic is true so it actually executes collider script every time.
Thanks.
Edit: Video was private instead of unlisted. Fixed now.
I guess i miss clicked private ins$$anonymous$$d of unlisted. Its fixed now.
Answer by MelvMay · Dec 28, 2015 at 05:00 PM
If you're going to disable/enable then by far the fastest way is to set Rigidbody2D.simulated to false. This tells the simulation to ignore the rigid-body, colliders, joints (etc) and doesn't require destroying/recreating stuff. As I said, very fast.
In terms of why your bricks are not 'hiding'; I'm not sure. As you said, you've verified that the script is called so it seems like it's nothing to do with the actual physics system.
If bricks are set kinematic all the time everything works perfectly. Thing is i want to add some bomb blocks that will apply some force when destroyed to all other bricks around and when I use them as non-kinematic they start to act weird and not hide/destroy.
I will try to set simulated to false, but I'm not home till tomorrow to give feedback.
I was pointed my video was private. Can you please watch it to see what's going on. I changed it to unlisted. Thank you.
Your answer

Follow this Question
Related Questions
Collider2D.GetContacts(List() contacts) works with OnColliderEnter2D but not with OnTriggerEnter2D 1 Answer
OnTriggerEnter2D and ladder won't be compatible 2 Answers
Disable click action when collision 1 Answer
Edge Collider issue 1 Answer
How do I set an object's velocity to the velocity of an object that collided with it? 0 Answers