For a better understanding of my game object structure :
Player (white capsule rigibody 2D)
— Rotate point (with the script above to make the arm rotating according to the mouse position)
--------Arm (black capsule with capsule collider 2D)
Ball (rigibody 2D and circle collider and boucing material 2D)
I’m looking for adding physics to the arm to throw the ball but currently the ball
passes through the arm when i move it fast.
Is there a unity component to handle this case? Should it be managed via code, such as with a Reflect method ?
You should never be modifying the Transform when using 2D physics, ever. This isn’t movement, it’s instantly changing the pose without travelling through the world.
The only things that “move” in physics are Rigidbody2D. Modifying the Transform does just that, modify the Transform. When the simulation next runs (no, the change isn’t instant) which is by default the fixed-update, the collider is recreated at the new angle. It’s recreated because the collider is fixed in relation to a Rigidbody2D. The only thing that can move is a Rigidbody2D.
This capsule should be on child with a Kinematic Rigidbody2D. You then use the Rigidbody2D API to cause it to move. In your case, it’d be Rigibdody2D.MoveRotation.
So a simple rule: Never modify the Transform when using physics to cause movement. Always use the Rigidbody2D API to get that done.
EDIT: Also note, when reading the pose of physics related GameObjects, read the Rigidbody2D.position/.rotation because it is the authority. This is especially true when using interpolation because the Transform will be moving from the old position to the current one so will be wrong.
Thank you for the quick and clear answer.
Can a rigidbody contain a child with a rigidbody? I’ve read somwhere that it is not recommended.
In my case i’m using a empty game object to create the rotation point, I assume that it is possible to configure the rotation point of a game obejct without creating a new game object ?
It’s not recommended to use Dynamic Rigidbody2D no because they are then in conflict. A Kinematic Rigidbody2D as a child will follow the pose of the parent but still allow you to use MovePosition/MoveRotation.
This is how “ragdolls” are set-up or how child animations work correctly.