Search Unity

2D coliders or triggers are making huge overhead if they have animated parent

Discussion in 'Physics' started by URocks, Feb 27, 2019.

  1. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159
    i tryed to link 100 triggers under animated parent and they have huge overhead inside profiler, even when i turn all of colisions inside physics colision matrix, so they should not colide with anything, there are still alot physics calculated in profiler.Why is that?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Hard to say exactly unless you provide the provide the profiler info.

    Could be that they are static colliders and because you're animating the Transform of the parent then the child colliders are being recreated due to the Transform change.
     
  3. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159

    i tryed everything, static, non static, coliders, triggers, they always have huge overhead in profiler if they are under animated parent.

    i filed the bug report (Case 1132443) please check if for yourself, also I am including image
     

    Attached Files:

  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    I had a look at your bug case and you're moving static colliders i.e. colliders that don't have a RB therefore should not be moved. Add a Rigidbody2D to the root and set it to be Kinematic which helps a lot.

    That said, you're moving a grid of 24 x 24 (576) BoxCollider2D each frame and from your previous post here I presume you're attempting to use colliders for some kind of particle-based simulation which won't work too well.

    At the very least, change them to be CircleCollider2D but it won't scale well either way.

    On my machine moving all those statics was taking around 8ms whereas adding a Kinematic body changed that to 3.5ms and changing the boxes to circles went down to 2.5-3ms.
     
  5. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159
    but isn it a huge overhead if all colisions are turned off and i am using only triggers, and even the colision matrix is not using any colision pairs, so there should be no physics involved at all
     
  6. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159
    also i quess, there should be some difference if i turn on or off trigger checkbox, but the performence is same, either way
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Depends on what you mean by "no physics involved". You're manipulating physics objects so physics is involved. If you mean collision response then there isn't any overhead as no contacts are produced. Relocating so many colliders instantly (teleporting them from position to position via the animator) is what is costing as well as you moving static collider geometry as I said above. Turn off the animator and look at the time spent in physics, it's practically zero.

    As well as following what I said above you could add the following script to move the root body which also helps make it faster:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Move : MonoBehaviour
    4. {
    5.     public float Amplitude = 5.0f;
    6.     public float Frequency = 3.0f;
    7.  
    8.     private float m_Angle;
    9.     private Rigidbody2D m_Rigidbody;
    10.  
    11.     void Start()
    12.     {    
    13.         m_Rigidbody = GetComponent<Rigidbody2D>();
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         var position = new Vector2(Amplitude * Mathf.Cos(m_Angle), 0.0f);
    19.  
    20.         m_Rigidbody.MovePosition(position);
    21.  
    22.         m_Angle += Mathf.Deg2Rad * Frequency;
    23.     }
    24. }
    25.  
     
  8. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159
    Nice and good to know that moving parents rigidbody, instead of moving transform is little bit faster, thank you for the tip.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Also forgot to mention but you could try turning on the multi-threaded 2D physics in the 2D physics settings however this would obviously scale depending on how many cores you had. Obviously that varies from device to device.

    Certainly on my machine here it goes from 3.5ms to 0.52ms simply turning it on.

    https://gyazo.com/dabb50efd235afd34a73ecefe8d426df
     
    Last edited: Mar 1, 2019
  10. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159
    I solved it with help of your code:

    I made new gameobject and animated it with animator how I wanted.

    then in my parent gameobject i removed all rigidbody components from children.

    I added rigid body componet to this parent and i added your modiffied script and linked myAnimatedTransform variable to animated gameobject

    Code (CSharp):
    1. using UnityEngine;
    2. public class test : MonoBehaviour
    3. {
    4.     public Transform myAnimatedTransform;
    5.  
    6.     private Rigidbody2D m_Rigidbody;
    7.  
    8.     void Start()
    9.     {
    10.         m_Rigidbody = GetComponent<Rigidbody2D>();
    11.     }
    12.  
    13.     void FixedUpdate()
    14.     {
    15.           m_Rigidbody.MovePosition(myAnimatedTransform.position);
    16.     }
    17. }

    As you can see there is no physics calculation involved in profiler while parent is moving all children trigger coliders, exactly like i wanted and triggers ares still working :)
     

    Attached Files:

    • psx.png
      psx.png
      File size:
      285.9 KB
      Views:
      616
    Last edited: Mar 1, 2019
  11. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159
    Explained maybe better: So the initial setup was animated parent (empthy gameobject with animator component) with lot of children gameobjects which each have 2d circle colider with trigger checkbox enabled and sprite renderer components.
    This was showing performance hit in profiler in physics.

    so I made new empthy gameobject, i copyed the animator from my animated parent to it, and deleted it from thatparent. So now the parent with lot of childrens is not moving. Only newly created Gameobject is moving.

    Then I added Rigid body2d component to my not moving parent and switch it do kinematic.
    I added the script above to it and in editor I fill the myAnimatedTransform variable with that animated gameobject.

    And now triggers works without physics overhead in profiler.

    Is it now clearer?
     
    Last edited: Mar 21, 2019