Search Unity

Question Is it bad to move static colliders by changing their localPosition?

Discussion in 'Physics' started by SeriouslyNot, Jul 7, 2020.

  1. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    I stumbled upon these tips from Unity here:



    "Never ever move a static collider". o_O

    I have this "Balls hitting bricks" game that i'm making, the bricks are a Prefab with BoxCollider but with no RigidBody because they are static on screen.




    The player shoots the balls to hit the bricks, but after the balls are done hitting and vanished through the ground; i move all bricks down to add a new row of bricks above them, i'm doing that by changing their localPosition directly. Is this moving static colliders?


    Do i have to add RigidBody to the bricks and set it to Kinetic?
     
    luizero00 likes this.
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    Yes, that's moving static colliders, but that information in the documentation is outdated. At least since Unity 5.3 you can move static colliders without rigidbodies and without incurring in any penalties.

    Edit: it has been like that since Unity 5.0
    https://unity3d.com/unity/whats-new/unity-5.0
     
    Last edited: Jul 7, 2020
    luizero00, protopop and SeriouslyNot like this.
  3. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    I’ve been trying to get Unity to change that docs page for years. They won’t do it.

    In any case, don’t add rigid bodies.
     
    protopop, SeriouslyNot and Edy like this.
  4. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    @Edy @AcidArrow

    So the way i'm moving them is right. Right?
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    If you don't need rigidbodies for something, don't add rigidbodies.

    From the testing I have done, adding rigidbodies when you don't need them always makes performance worse, regardless of how you move them.

    I think this is a case where the manual is flat out wrong, but somehow Unity stubbornly refuses to correct its manual. (for 3d physics) There isn't really a concept of "moving static colliders" any more.
     
    Last edited: Jul 7, 2020
    SeriouslyNot likes this.
  6. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,560
    This is really good news but kind of mind blowing for me

    Based on the docs I have iskinematic rigid bodies attached to all my moving colliders for years - on animals , people etc. to avoid the moving colliders penalty.

    Can I actually remove these rigid bodies safely? I'm asking because it's so in grained in my workflow and this is the first time I am hearing this so it's almost hard for me to believe. Is there some official mention of this anywhere?
     
    SeriouslyNot likes this.
  7. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    Based on your description yes, you can move them that way.

    Note that modifying position or localPosition means "teleporting" the colliders. As balls are done hitting moving the colliders won't cause any problem. Moving them while there are physics interactions in place could cause unexpected physics effects (i.e. balls bouncing in unexpected directions).

    As long as you're not using them and they aren't causing any physics interaction, then yes, you should be able to remove them without any drawbacks.

    Official mention:
    https://unity3d.com/unity/whats-new/unity-5.0

    upload_2020-7-7_17-27-8.png

    This has been like that since Unity 5 (not since Unity 5.3, as I wrongly stated in my previous post).
     
    luizero00, protopop and SeriouslyNot like this.
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    Some more info here:

    https://forum.unity.com/threads/tri...ng-rigidbodies-from-trigger-colliders.649402/ (read posts by yant)

    Here is a blog post saying static collider cost is gone now : https://blogs.unity3d.com/2014/07/08/high-performance-physics-in-unity-5/

    Here is a bug report for the documentation bug from 2016 (not by me) https://fogbugz.unity3d.com/default.asp?758509_lnn1i1o4hdv1dlp8

    Here is another bug report by me from 2017
    Screenshot 2020-07-07 at 7.35.50 PM.png

    AFAIK, if you are moving the transform many times per frame and autosync transforms is set, it may be faster to add a rigidbody and use rigidbody to move the colliders around, maybe.

    One thing's for sure, "moving static colliders is super expensive" is simply not true any more and it hasn't been since Unity 5. Why Unity keeps that "tip" into their manual is baffling to me.
     
    CandraWi and SeriouslyNot like this.
  9. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    @AcidArrow Should i use Auto Sync Transforms in this game?

    I just looked into my Physics2D settings, and it's not checked.
     
  10. Modafuka

    Modafuka

    Joined:
    Nov 21, 2019
    Posts:
    45
    Just test it man, i move 123 static colliders + 1 rigid body in my mobile game and it fine.
     
    CandraWi likes this.
  11. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    I've tested in a 2D environment and moving static colliders is more expensive than moving kinematic rigidbodies (the difference becomes massive as I bump the number to thousands).
     
    SeriouslyNot likes this.
  12. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    so i should attach kinematic RigidBodies to the Bricks?
     
  13. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    Yeah, I think so.
     
    SeriouslyNot likes this.
  14. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    Just to clarify, my and Edy's comments are about the 3d Physics. For Physics2D, the add rigibody advice is still valid.
     
    SeriouslyNot and Edy like this.
  15. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    Yes, I can speak for 3D physics only.
     
    AcidArrow and SeriouslyNot like this.
  16. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    Even if i'm just teleporting them (setting their transform's localPosition)?

    Explanation:
    There's a function that i'm calling every-time the balls are gone, the function teleports down all cubes by one row, and adds one row of cubes at the top. (check the photo above)
     
  17. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    I think teleporting them in a single step shouldn't be noticeable, even if it incurs in some penalty. This kind of issues typically happen when animating them or moving them every frame.

    Just try it and watch the Profiler for any sudden overhead that may cause any noticeable hiccup. If you don't observe anything special, then go ahead without rigidbodies.
     
    AcidArrow and SeriouslyNot like this.
  18. JohnnyFactor

    JohnnyFactor

    Joined:
    May 18, 2018
    Posts:
    343
    The docs have finally been updated. I feel confident removing rigidbodies from non-trigger colliders but unfortunately the internet is filled with forum posts, blog posts, and Unity Answers with the old information, so this will continue to be a confusing mess for years to come.

    https://docs.unity3d.com/Manual/CollidersOverview.html
     
    luizero00, CandraWi and Edy like this.
  19. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    And it only took them... 4 years?

    Frankly I'm starting to worry that the behaviour has probably changed by now, since I'm pretty sure the docs are written by trolls.
     
  20. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    Still under the average of 5 years. And still has confusing or incomplete information (i.e. kinematic colliders can raise collision events if configured in the Physics settings, moving kinematic rigidbodies should use MorePosition / MoveRotation instead of Transform).
     
  21. JohnnyFactor

    JohnnyFactor

    Joined:
    May 18, 2018
    Posts:
    343
    They just released a physics video with updated information. In the section "Moving Static Colliders" (7:23), they recommend not using rigidbody components.

     
  22. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Figure out if you're talking about 2D or 3D physics then discuss.

    The static optimization is talking about 3D physics because it had an overhead when doing so. 2D physics hasn't changed since it was first integrated. If it moves, use a Rigidbody2D. If that hurts your spider senses then do as you like, we're forced to support moving via Transform. There, Static means exactly that but we can't control what you feel you must do. Kinematic is your "immovable but moveable" mechanism there.

    The reason it's like that in 3D physics is because you cannot explicity select a Static body-type so it's only done implicitly by not adding one. That's different than 2D as you have a BodyType field.

    I don't work in 3D physics nor deal with the docs or tutorials though.
     
  23. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    I wonder if the docs providing completely wrong information for many years has something to do with people being confused with how Unity's physics work. Or the Unity docs conflating its own systems.

    But anyway, if you read a few posts, you'll see we figured out the 2d 3d stuff a while ago, thanks.
     
  24. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Could you provide the link to the docs for 2D physics that tell you to move a Static 2D collider or something similar? I'll get them changed. 2D has not changed or added confusion because it has always been the same. The confusion here is confusing 2D physics with 3D physics.

    Sounds like you're suggesting I didn't and that I don't spend a considerable amount of my time answering posts to clear-up misconceptions and confusion.

    The first post here is showing information discussing 3D physics and then applying that to 2D physics. The term "Unity's Physics" is completely ambiguous which makes things worse.
     
  25. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    The last few posts are clearly talking about the 3D Physics, before you popped up and brought up 2D again.
    You do, but you always seem to imply that the onus for the confusion is on the users and not how badly Unity handled communication and docs and tutorials for the last 5-6 years.
    Unity Physics refers to the DOTS package, obviously.

    The docs and tutorials have been conflating "physics" for years. Sometimes it's Physics (meaning the 3D ones) and Physics 2D. In other places it talks about Physics and Physics 3D.
     
  26. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Yep, discussing 2D physics on a thread that started with 2D physics and contains mostly 2D physics posts. I'll let you get back to 3D physics here.

    I'll get back to apparently implying all users are at fault.
     
  27. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    Thanks.
     
    MelvMay likes this.
  28. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    I love it when a plan comes together. LOL ;)
     
  29. abandon_games

    abandon_games

    Joined:
    Sep 1, 2017
    Posts:
    38
    no need to get hostile, we're all on the same team here are we not? I understand frustration, but it's not productive, so keep it to yourself if you can't find a way to make it constructive

    regarding 3D physics (yes, it's true, this thread was originally concerned about 2D physics), there's some helpful information on this thread where I've asked a few additional questions
     
  30. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Thanks. I mean, this was early last year but consider me scolded again. ;)
     
    AcidArrow likes this.