Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity Physics2D recreate each collider when changing a transform.

Discussion in 'Scripting' started by Spidyy, May 10, 2014.

  1. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    Hello there.

    While I was looking to enhance the performance of my game, I saw something disturbing with the FixedUpdate stuff.

    I need to move my objects each FixedUpdate to gain a better precision over what I'm doing, and in the profiler, it is translated as this :

    $Performance.jpg

    If you look at the expanding under Gameboard.FixedUpdate(), it is quite explicit.

    In my FixedUpdate, I directly change the position of my game object. And each time I do this, Unity destroy all colliders and recreate them at the new position, and with or without rigidbody attached.

    The only way to avoid this process of destruction and creation, with cause a huge performance drop, is to have a rigidbody attached and change its velocity.

    So, intended behaviour?
    Some other way to move a gameobject without risk?
     
    Last edited: May 10, 2014
  2. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    I bump this, it cause me a lot of modifications on my scripts I'd really like to avoid. :s
     
  3. BPF2014

    BPF2014

    Joined:
    Jul 3, 2014
    Posts:
    1
    Hi, did you find a solution, because we are having the same problem. It's costing us nearly 100ms / frame when built for iOS.

    Thanks,

    BPF
     
  4. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    same here
     
  5. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    Hey,

    Well, the only solution I found is :
    - You must not move a collider without its Rigidbody.
    - You must not move a collider, with or without a rigidbody, from the FixedUpdate function.
    Do either of those two and your collider will be destroyed and recreated to its new position by Box2D. :(
     
  6. et_cloudcade

    et_cloudcade

    Joined:
    Jul 8, 2014
    Posts:
    8
    same here, we had a freeze on ios and android because the re-genetation crash for one 2d collider (bad triangles?) and only on mobile.. take us long to figure out. I would like know the reason behind this perf issue: destroy all colliders and recreate it at runtime.

    for us, the fix will be to remove all 2d physic component of our game, since we only use it to make 2d raycast, instead we will make an in house raycast solution.
     
  7. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    Well, a simple solution is to ensure every collider or group of collider have a rigidbody attached to it and do the transform through the rigidbody's gameobject.

    Just never move a collider alone. As it is seen as a static collider by box2D, it will be deleted and recreated somewhere else.
     
    Last edited: May 26, 2015
  8. Mikeysee

    Mikeysee

    Joined:
    Oct 14, 2013
    Posts:
    155
    Oh my, this is quite a gotcha! This has been causing me serious issues!
     
  9. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Meet The same problem....Any good method to fixed this? this is a big hole in develop 2D game on mobile....
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Ridgidbody.MovePosition should take are of it.
     
  11. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    5,982
    It doesn't, I'm running into the same perf glitch despite using MovePosition because the rotations are 3D,

    upload_2017-6-14_22-47-20.png which

    Internally Box2D seems to move the collider anchors position and rebuild the collider profile with the unpleasant result of 2ms for only 300 colliders.

    It's a shame because Box is super fast when doing purely 2D rotation, the documentation should reflect that it's ill suited for 2.5D games where rotation is in non Z axis.