I’m having trouble understanding the limitations when working with Rigidbody2D in combination with certain transform and GameObject operations. I know that it’s generally advised not to set transform.position/rotation directly when controlling a Rigidbody2D object, as the Rigidbody2D documentation makes clear. Instead, you’re supposed to move the object using Rigidbody2D methods like MovePosition() or Rigidbody2D.position, which makes sense.
However, I’ve run into several pitfalls while working with Rigidbody2D.position that aren’t explicitly covered in the documentation. I feel like there are further limitations I’m missing, and I’m hoping for clarification from the community.
Issues I’m Struggling With:
GameObject.SetActive()andRigidbody2D.positionin the Same Frame: When I try to deactivate an object (SetActive(false)) and set itsRigidbody2D.positionin the same frame, the position doesn’t stick and seems to reset in the nextFixedUpdate. I’ve tried usingPhysics2D.SyncTransforms()right after setting the position and before togglingSetActive, but this hasn’t helped.- Reparenting the Object and Setting
Rigidbody2D.position: When I reparent an object by changing itsTransform.parentand set theRigidbody2D.positionin the same frame, the position also resets, as discovered in this Unity forum thread.
Main Question:
Given these limitations, should all Transform operations (or operations like SetActive()) be avoided when dealing with Rigidbody2D objects? Specifically:
- Should we avoid any direct
Transformchanges or operations likeSetActive, or do we just need to stagger these to different frames to avoid conflicts? - Should I only work through the
Rigidbody2DAPI for all movement and state changes?
I ask this because it feels like it enforces a fairly rigid project structure, where all physics objects would need to remain at the root of the hierarchy or remain active for their entire lifecycle.
Notes on Physics2D.SyncTransforms and autoSyncTransforms:
- I’m aware of
Physics2D.autoSyncTransforms, but the project settings note that this is being deprecated. I’ve also triedPhysics2D.SyncTransforms(), but it doesn’t seem to help with theSetActive()issue. I came across this post where Unity staff member @MelvMay mentions that it shouldn’t really be used, as it’s inefficient and bad design.
My Attempts and Searches:
I’ve searched through Unity Learn, the manual, and API docs, but I haven’t been able to find detailed info on how to handle these cases properly. Most of the relevant information I’ve found is scattered across random forum posts, and I feel like I’m missing something fundamental.
If anyone has any experience with these limitations or can point me to further reading or best practices, I’d greatly appreciate it! Links or advice from others who have navigated these issues would be incredibly helpful.
Thanks in advance!