- Home /
AddForce to RigidBody2D on the same frame as SetParent(null) causes small jitter
I've got a Rigidbody2D sprite that's a child of a rotating circle (the circle does not have a RigidBody2D). When I click the mouse in Update(), I unparent the sprite using SetParent(null) and apply an outward force using AddForce(force, ForceMode2D.Impulse) to the sprite.
When I set the timescale low, I see the sprite jumps a short distance, then pauses for 5-10 frames, and then starts to move smoothly as expected. The short pause is not expected.
When I simply remove the SetParent(null) call, I do not see this short pause. Unfortunately I can't use this as a practical solution as I do need to unparent the sprite.
I've tried calling Physics2D.SyncTransforms() after unparenting, changing the interpolation type for my Rigidbody2D and adjusting Time.fixedTimestep but nothing changes. I also tried waiting a few frames to apply the force after unparenting, but it seems I need to WaitForEndOfFrame() 15-20 frames for this to remove the jitter.
Does anyone know if it's "invalid" to unparent an object the same frame as trying to apply physics?
Answer by Pawl · Aug 24, 2019 at 05:56 AM
To answer my own question, this seem to simply be a limitation with how Rigidbody2D and transform parenting works.
To work around this, rather than change the parent of my Rigidbody2D object, I create a dummy GameObject and dynamically add it as a child of the rotating circle. Then in Update(), I simply set the position and rotation of my Rigidbody2D object to match the dummy object. This removes the jitter while still allowing me to use the Rigidbody2D for dynamic physics when needed.
Answer by Parokonnyy · Aug 23, 2019 at 10:05 AM
Try to use FixedUpdate instead of Update. For more information: https://learn.unity.com/tutorial/update-and-fixedupdate?projectId=5c8920b4edbc2a113b6bc26a#
Unfortunately moving AddForce() and/or SetParent(null) to FixedUpdate() didn't make a difference. I think this has to do with parenting but I don't understand why.
Your answer
Follow this Question
Related Questions
Detect onMouseDown() on children colliders of a rigidbody parent 2 Answers
Platformer2D Player sticks to platform corners 0 Answers
More realistic physics on child collisions 2D 0 Answers
How Do I Make a Rigidbody2d keep its force and move at a consistent speed? 2 Answers
Rigidbody2D.position and Rigidbody2D.MovePosition() 0 Answers