Search Unity

Auto Sync Transforms

Discussion in 'Physics' started by hemedah94, Dec 26, 2018.

  1. hemedah94

    hemedah94

    Joined:
    Oct 9, 2018
    Posts:
    8
    Can someone Explain to me Auto Sync Transforms ??

    It seems to be turned off by default in newer unity versions, bugged my code and caused me a ton of frustration and debugging to find this out?

    According to unitys docs: To avoid affecting performance, set autoSyncTransforms to false if you want to perform multiple Transform changes and queries in succession.

    Is this supposed to be done manually now? if so HOW?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Prior to this option, every time you modified a transform it auto-sync'd those changes to all respective physics objects immediately (prior to returning from your transform change). If you're making multiple changes to transforms then this can be expensive. It was actually quite common to see projects where a single transform set the position and rotation separately, even the scale. Each one of these caused physics updates.

    With the option on, this legacy behaviour occurs for backwards compatibility. With it off however, the transforms are only sync'd prior to the simulation being run.

    You also have the option to sync them manually whenever you like however that should be done with caution as it can affect performance.

    Internally, manually syncing transforms is exactly what happens automatically prior to simulation being run. Whenever you change a transform (in any way) then it is flagged as changed. Internally, components can register their interest in such changes. We can then ask what transforms have changed for certain component types and process them. This is effectively what the manual sync-transforms is.

    The above applies to both 2D & 3D physics. Note that this change was required for other non-physics reasons internally and all other systems use it. The difference in physics is that we had very specific behaviour when a transform changed whereas most other systems didn't. Due to this we had to ensure that we kept backwards compatiblity but also had a way to ensure we could meet the requirements of the deferred transform sync.

    Hope that helps to clarify it.
     
    Last edited: Jan 28, 2020
    Edy, tinyant, mjc33 and 8 others like this.
  4. SkymapPatrick

    SkymapPatrick

    Joined:
    Aug 30, 2019
    Posts:
    4
    Hey @MelvMay!

    I am wondering how to properly handle updating a legacy project and turning this off. A contract I am working took a project from a version prior to 2017, and is now in 2019. We need to up performance for some of the platforms we are targeting.

    Thank you!

    Patrick R.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    I'm not sure I follow the question, you can simply turn it off. If the project involves setting a Transform then immediately querying physics then it's a bad thing to do anyway. Additionally you should consider that setting the Transform on a GameObject with physics components (something I make a forum post on about several times a week) is something you should never ever do as the Rigidbody2D is in charge of the Transform; it's what it's there for.

    If the project doesn't do the above then just turn-off AutoSyncTransforms as it's checking for transform changes which you are not doing (or shouldn't).

    If you want to change the body pose then you should always use MovePosition/MoveRotation. If you absolutely must change the position of a body immediately (not sure why you'd ever need to do this really) then set the body.position property.
     
  6. SkymapPatrick

    SkymapPatrick

    Joined:
    Aug 30, 2019
    Posts:
    4
    I appreciate the quick response! My question comes down to, I'd like to turn this off to increase performance, but since it is a legacy project, turning it off causing some unwanted results.

    Sounds like somethings are doing the incorrect things and turning off the AutoSync is showing their true colors. I will take a look into the objects that are having issues and make sure that they are handled correctly.

    Thank you again!
     
  7. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,795
    What if we’re only using Colliders without rigid bodies, how should we move those?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    I can only speak for 2D physics but when you "move" something in physics you should only ever do this by manipulating a Rigidbody(2D). If you're doing anything via a Transform then you're doing it wrong and perfomance will suffer. Of course we are forced to implement and catch this situation and will update stuff but it's still bad in every way.

    So with that known, it should be clear that by modifying the Transform on a GameObject that only has a Collider is bad and is in-fact static. If it's supposed to move under your control then you should use Kinematic. If it never moves then Static. If it responds to forces etc then it's Dynamic. A Collider without a Rigidbody(2D) is Static.

    If you absolutely must have them Static and must move them then add a Rigidbody2D and set the body-type to Static. If you don't add a Rigidbody2D then colliders are added to the hidden static ground-body that lives at the world-origin. In physics, bodies move, not colliders. By doing what you suggest, the collider needs to be recalculated and work done to move it. With a Rigidbody2D on the same GameObject then you can set the Rigidbody2D.position/rotation properties to get it to move.
     
    Last edited: Nov 21, 2019
    AcidArrow likes this.
  9. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,795
    Thank you for the reply once again, but we've arrived at this point before.

    I want a similarly clear answer for 3d physics, I've been trying for years on the forums.

    I even opened a ticket ( 873057 ) almost 3 years ago, with a minimal repro that shows that adding moving colliders with rigidbodies is slower than just moving colliders and asking that if it's a bug it should be fixed, or if this is the way it works you should update the manual (which you did, BUT IT STILL SAYS THE SAME THINGS). QA says they reproduced it and then... nothing.

    At this point I can only conclude that someone changed stuff in 3d physics 3-4 years ago, then left Unity and no one knows what's going on any more.

    In any case, thank you for replying, it may not be what I was looking for, but I have a pretty good understanding about how 2D Physics in Unity work at this point.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    I've seen this question asked and answered many times on the forums for 3D physics by @yant. Hopefully this mention here will get you the info you require.

    Not true at all.

    I checked this case and it's not assigned to 2D or 3D physics teams, it's assigned to the docs team (still).
     
  11. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    With the setting off :
    I am currently in the situation where I have the player in the middle of the screen (position 0,0), and I instantiate an enemy out of screen (supplying the position in the Instantiate() method, albeit I believe that's the same as changing the transform afterward), but the physics still thinks the rigidbody is in the middle for that physics frame...thus, damaging the player once. Is this intended behaviour? @MelvMay
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Probably best to create your own thread and post some code. Sync transforms has nothing whatsoever to do with a rigidbody being positioned where it's created. It'll always do that.
     
    ihgyug likes this.
  13. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    @MelvMay Are we still suppose to do that in 2019.4 (LTS) (adding RigidBody2D to static colliders just to move them once in a while)? I thought that moving static colliders does not cause performance penalty anymore.
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    This hasn't changed since day#1 and won't change. A Collider2D with no Rigidbody2D means you want it implicitly static meaning you're saying it isn't going to move and it'll be created on the hidden static ground body that lives at the world origin. Of course we know that devs will still move it (and even use the Transform which is bad) so we have to deal with it. Because 2D colliders live in the space of the body, such a colliders geometry is in world-space so changing its position means it has to be recreated completely because we can't move the ground-body.

    Quite simply, anything in 2D physics that moves means moving the body so move the body but again, don't modify the transform ever.

    Also there's a huge interpretation of what "moving" means. Instantly teleporting its position is far different than moving it through space. Anything that moves should be Kinematic so use that no matter how often it moves. If for some other reason you must have static then add a Rigidbody2D and set it to static body-type. There's no difference then not adding a Rigidbody2D component apart from the fact that you can directly control its position as you have a component to work with.
     
  15. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    So, is there a performance difference when changing position of Collider2D without Rigidbody2D through transform vs Collider2D with Rigidbody2D through Rigidbody2D? (Each frame)

    How big that difference is? Cost of recreating collider and re-inserting it back to the data structure?
     
    Last edited: Jul 14, 2020
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    As I stated above, a collider with no Rigidbody2D is created against the static ground body so changing its position requires changing its geometry so a full recreate of the collider is required but this wouldn't be a problem because not adding a Rigidbody2D means you're stating it won't ever move right? ;)

    If there's a Rigidbody2D then you're only changing the Rigidbody2D position/rotation and the collider isn't touched.

    Impossible to give you a figure. Less work is more. Just add a Rigidbody2D, there's no penalty so I'm not even sure I follow why it's even an issue.
     
    xVergilx likes this.
  17. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Well, there's always a memory cost of adding extra component. Its CPU vs memory as always.
    You're right though, for something that is moved every frame its probably worth adding Rigidbody2D.
     
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Sure for heavyweight components but for a Rigidbody2D set to static it's practically nothing but yes, it's not zero although I've seen solutions like add a Rigidbody2D, move it then remove it which is beyond silly. There's zero extra CPU overhead when it's created and up and running apart from there being another b2Body in Box2D so unless you're creating hundreds of thousands it's not even an issue. Box2D pretty much ignores static bodies and only looks at them when there's a Dynamic body interacting with them.

    I think it's worth reiterating for those who look at this thead and TLDR: The only things that move in 2D physics are bodies not colliders so if it moves, you should be moving a body. Anything else is "faked" and has a FAR higher cost such as "moving" a collider.
     
    xVergilx likes this.
  19. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    @MelvMay

    I'm teleporting the gameObjects:

    gameObjects.transform.localPosition = new Vector2(x, y);

    I'm doing that outside any Update function, just a function that instantly moves (teleports) all cubes down.

    Does that count as moving?

    Do i still have to add RigidBody2D(s)?

    Sorry if this became like a broken record :D, but i'm trying to understand everything i'm doing and note things down.
     
  20. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    TBH I've explained it above twice so I just don't know how to explain it any other way. You don't have to do anything in any specific way. Some ways consume more CPU time because they're just us supporting actions you really shouldn't be doing.
     
    SeriouslyNot likes this.
  21. OneManEscapePlan

    OneManEscapePlan

    Joined:
    Oct 14, 2015
    Posts:
    222
    This blog post says that the cost of moving static colliders was greatly reduced in 2014, so it has changed in the past. IIRC the profiler used to include a performance warning about moving static colliders but this was removed after the cost was reduced. The blog post doesn't make it clear whether the cost is now the same as moving a Rigidbody, or if there is still some additional overhead. I don't recall being able to find any information in the documentation about whether there is still a performance cost for moving colliders with no Rigidbody attached.

    I think the reason that we often see developers moving around 'static' colliders is that the desired solution is counterintuitive. When most people hear the term "physics" in the context of a video game, they think "forces and joints". Rigidbody/2D is the component that adds support for forces and joints, so for any moving object that doesn't require forces or joints (e.g. a platform moving at a fixed speed), a natural thought process for a beginner is "this object doesn't use physics, so I can improve the performance by not including a Rigidbody." The "is kinematic" checkbox is also counter-intuitive, since it disables almost all of Rigidbody's normal features, which can easily lead to the thought process "what's the point of putting a kinematic Rigidbody on this object if a kinematic Rigidbody doesn't do anything?"

    The Rigidbody documentation makes no effort to explain the benefits of using a kinematic Rigidbody vs. not including a Rigidbody at all, and even suggests that kinematic rigidbodies are mostly used for ragdoll effects.
     
    Last edited: Aug 11, 2020
    NashBits and SeriouslyNot like this.
  22. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    That blog post is obviously referring to 3D physics which is the physx offering. I'm clearly referring to the 2D physics which is the Box2D offering. The two engines are different offerings. What I said is correct.
     
    SeriouslyNot likes this.
  23. OneManEscapePlan

    OneManEscapePlan

    Joined:
    Oct 14, 2015
    Posts:
    222
    Hmm, good point.
     
  24. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Since this is the first result on Google about autoSyncTransforms, and it's very well explained by @MelvMay, I wanted to add a follow-up question here to complete the information.

    How autoSyncTransforms (enabled) works with Rigidbody(2D) interpolation? Here's the situation:
    1. A physics update has just happened. Both Transform and Rigidbody(2D) have the same position.
    2. Several Update cycles happen afterwards. In each Update, the interpolation modifies the Transform.
    3. In these Updates some physics queries happen.
    Since the Transform is changed (by interpolation) and physics queries happen right afterwards, does this mean that the physics world is synced in each Update to match the interpolated positions?

    I ask because I've seen that with autoSyncTransforms enabled there are sync-transform calls when calling Physics.Raycast from Update:

    upload_2023-11-10_18-18-39.png