- Home /
Performance issues when moving 2D colliders by code.
Hi, I am making a 2D ball and paddle game, similar to breakout/arkanoid.
Some of my blocks move side to side, spin around their own pivot or rotate around a point. Their Rigidbodies2D are marked as kinematic and fixed angle, since all I want is to detect the collision with the ball, I don't want the ball to be able to push them.
The static ones work great, but the ones that move, specially the ones with more than one collider and the ones with polygon colliders instead of circle/box simple shapes, cause a huge performance drop. I've tracked it down to "Physics2D.XXXColliderCreate" (where XXX is the collider type). So it seems that every time I change the position or rotation of the gameobject by code, the collider (or colliders) is created again.
I have tried changing the code from a "Transform.RotateAround" to a "Tansform.Rotate" and a "Transform position = X", it doesn't solve the problem.
I have alto considered making them non Kinematic and moving them through physics, but the math to get the same movement is way more complicated and even if I make the ball ultra light and the blocks ultra heavy, they will still move a little.
I am quite surprised by this problem, since what I need to achieve is rather basic. Is there any way to get detect collisions between two objects, one moved by code and the other one affected by physics?
Answer by EvilTak · Dec 06, 2014 at 09:29 AM
Set your rigidbodies as kinetic, use rigidbody.MovePosition() and rigidbody.MoveRotation().
That solved it, I adapted the code to use those functions and now the performance of moving blocks is almost the same of the static ones.
Your answer
Follow this Question
Related Questions
Performance of player detection: Colliders/Triggers vs OverlapCircle 2 Answers
why Raycast2D doesn't work properly? 0 Answers
Weird behaviour after Collision? 0 Answers
Is Overlap Circle Supposed to return inactive colliders? 1 Answer
Passing Array to normal Raycast and using RaycastNonAlloc ¿Same thing? 1 Answer