Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question 2D Physics: Auto Configure Connected Anchor not working and is changing on the fly at runtime

Discussion in 'Physics' started by reinfeldx, Nov 16, 2021.

  1. reinfeldx

    reinfeldx

    Joined:
    Nov 23, 2013
    Posts:
    164
    I'm attaching two 2D Rigidbodies with a Hinge Joint and Auto Configure Connected Anchor set to true.

    Problem:
    when I move/translate the main anchor point at runtime, the automatically-set connected anchor point changes as well, resulting in a disconnected effect until I stope moving/translating.

    Manually setting the connected anchor works around this problem, but isn't a great solution for me. This comment looks like the same problem, but I'm not sure and it's very old: http://answers.unity.com/answers/1228400/view.html

    Is this a bug? Am I doing something wrong?

    Here's a video of what I mean (see the HingeJoint2D Connected Anchor values).

     
  2. reinfeldx

    reinfeldx

    Joined:
    Nov 23, 2013
    Posts:
    164
    Solved my problem. Even though the rigidbody on the main anchor was set to kinematic, one shouldn't move a rigidbody with transform.Translate(). Moving it with Rigidbody2D.MovePosition() works.

    From the docs: "Note: MovePosition is intended for use with kinematic rigidbodies."
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    True. You should never modify the Transform as it overrides the primary role of the Rigidbody2D itself which is to write its pose to the Transform. Even worse, this instant teleport doesn't work with interpolation (you wouldn't want it interpolating a long distance for instance) and it also is jarring to the solver, joints etc.

    Rule#1: If it moves in 2D physics, talk to a Rigidbody2D to get it done. :)
     
  4. reinfeldx

    reinfeldx

    Joined:
    Nov 23, 2013
    Posts:
    164
    @MelvMay Quick thing I just noticed: moving the kinematic Rigidbody2D by animating its Transform.position seems to work okay (at least it doesn't display the hinge-break behavior in my original question). Why is this? Is it still a bad idea in the context of the 2D physics system?
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    Doesn't matter who is doing it or that you might not see an issue, the same thing applies. It's also worse performance-wise.

    I cannot see why you think it's fine but if you do and you're not currently seeing an issue then carry on but I would suggest you don't do this. You should understand that because users expect "something" to happen, we're forced to gracefully get that Transform change back to the Rigidbody(2D) but we're doing that so you don't go "why does this not work!!". The Rigidbody(2D) should never have to read the Transform, only write to it.

    It's always the wrong way to do it no matter what situation you create.

    Lots of devs ignore this, then later it catches them out and have to work around other stuff such as interpolation not working, too many contacts being searched for, collision tunnelling all because that simple rule is ignored. :)
     
  6. reinfeldx

    reinfeldx

    Joined:
    Nov 23, 2013
    Posts:
    164
    @MelvMay Well, I don't know if it's fine or not, which is why I asked if it is a bad idea. I'm simply trying to better understand how the physics system differentiates between Translate() and animating a Transform's position because I'm seeing two different behaviors.

    If this is directed at me, you're misreading my tone here.

    From the docs: https://docs.unity3d.com/2021.2/Documentation/Manual/class-Rigidbody.html
    "Is Kinematic—If enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform. This is useful for moving platforms or if you want to animate a Rigidbody that has a HingeJoint attached."

    Maybe that wording should be revised, because I think the most direct reading of that entry in the docs is "if you want to move a kinematic rigidbody, don't use the physics engine, manipulate its transform." I'm not the only one who finds it misleading: https://forum.unity.com/threads/mov...le-unitypackage-included.361244/#post-2345737

    Edit: I notice that the Physics 2D docs are clearer about this, but I'm guessing the subject we're discussing applies to 3D physics as well. I'm used to working with the 3D system, which might be part of the confusion here.
     
    Last edited: Nov 17, 2021
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    I said originally...
    This is the problem with docs, even if that's posted all over the place then the question of, "But what about when I do....". The rule is without exceptions.

    Absolutely not directed at you at all. You would be surprised at how many hoops we have to jump through though to support terrible behaviour because if we don't, users become very unhappy. This is why we have situations where Unity will still do something but it's still bad practice. Certainly not directed at you or anything.

    Maybe but that's 3D physics, not 2D physics which I thought we were talking about here. But note, Kinematic bodies won't be affected by joints which is also the subject here.
     
  8. reinfeldx

    reinfeldx

    Joined:
    Nov 23, 2013
    Posts:
    164
    But what about when you animate a Rigidbody2D with Animation.animatePhysics turned on? https://docs.unity3d.com/2021.2/Documentation/ScriptReference/Animation-animatePhysics.html

    2D Kinematic docs describe Kinematic as "Should this rigidbody be taken out of physics control?" This entry also directs you to the 3D Kinematic docs for more information, where it says this: "The rigidbody will be under full control of animation or script control by changing transform.position." Just pointing out that I don't find the docs to be particularly clear and consistent about moving Kinematic rigidbodies.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    It uses MovePosition/MoveRotation. It just changes when it modifies the bodies.

    I can see that.
     
    DungDajHjep likes this.