Search Unity

Discussion How to ensure that child objects follow the parent object?

Discussion in 'Scripting' started by omerfirat, Nov 20, 2022.

  1. omerfirat

    omerfirat

    Joined:
    Aug 1, 2017
    Posts:
    9
    Hi everyone,
    I would like to consult you about an event that I do not fully understand.

    I had 100 cubes in my scene and I moved each cube in the same direction on the Z axis through the same code. Since the number of cubes is high, I naturally experienced a drop in fps.
    After that, I put 100 cubes in 1 parent object and with my code I only moved the parent object in the same direction on the Z axis.

    The result was the same in both operations, but just moving the parent object was much more performant.
    What I can't understand here is that the cubes in both cases can give different results even though the world positions change at the same rate.

    I can't understand how any child object does the parent tracking, but it works much more optimized than a normal tracking.
    Does anyone know the reason for this situation?
    Thanks.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    The whole game engine is centered around the concept of a "Transform Stack" where pretty much ALL things are calculated based on their parent transform. A "transform" is always present on every single game object in Unity, it's that important.

    Even though I wrote this about 3D, this post I made some years ago is still relevant for 2D:

    https://forum.unity.com/threads/what-is-transform-forward.338384/#post-3684166
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    You're posting in the physics forum but you're not mentioning anything related to physics from what I can tell. Are you simply talking about the Transform system? That isn't physics.

    If it's just Transform stuff then I can move your post to the Scripting forum.
     
  4. omerfirat

    omerfirat

    Joined:
    Aug 1, 2017
    Posts:
    9
    You are right, sorry about that. You can move the topic to the relevant section.
     

    Attached Files:

    MelvMay likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Done. I've left a redirect from the old post to here for you.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,751
    Don't waste too much mental energy trying to reason about how closed-source programs run internally.

    For one, you're not likely to guess correctly, and for two, it might change at any time.

    Instead, apply good engineering principles to understand the public API provided by Unity and to use it parsimoniously to achieve the results you want.

    If after careful engineering you find you have a performance bottleneck, use the profiler to begin your investigations.

    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    A child doesn't track the parent, that's an incorrect mental model. A transform stores the local-position, relative to its parent only. If a parent changes, nothing needs to happen to the child because it's still at the relative location position (and rotation/scale).

    Therefore, 1 operation is quicker than 100.

    You should see this when you look at a child. The position might be (0,0,0) but that's obviously not where it is.

    In-fact, if you switch the inspector to Debug view you can see the exact fields it stores.