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

Un-parenting entities

Discussion in 'Entity Component System' started by gamayun, Apr 13, 2020.

  1. gamayun

    gamayun

    Joined:
    Nov 20, 2012
    Posts:
    34
    Hi,

    I have some entities that are parented to a moving/rotating entity. At one point I want to un-parent some of those children entities. When I do so for each entity to unparent:
    Code (CSharp):
    1.                 PostUpdateCommands.RemoveComponent<Parent>(en);
    2.                 PostUpdateCommands.RemoveComponent<LocalToParent>(en);
    3.  
    then the entities are free of their parent, BUT they have a rotation that makes them spawn away from where they were the previous frame, and not staying put
    I know it has to do with TRS and such but I can't seem to find a solution.
    Just removing the Parent and LocalToParent isn't enough in itself? Anyone has a solution for me?
    Happy Easter, and stay safe at home coding! :)
    Cheers,
    Thierry
     
  2. alemnunez

    alemnunez

    Joined:
    Mar 15, 2023
    Posts:
    3
    You probably solved this already, but for future people ending up here:

    Unparenting preserves the "local transform" component information, but now the system that translates the "local to world" transformation matrix will just use that "local transform" as the world transform of the entity (because now there is no parent to take into account).

    So when you unparent an entity, you need to also calculate the new "local transform" (using the actual world transform of the entity before the un-parenting)