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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Offset mesh position in Entity

Discussion in 'Entity Component System' started by rjohnson06, Oct 7, 2018.

  1. rjohnson06

    rjohnson06

    Joined:
    May 27, 2017
    Posts:
    17
    I have a stupid question. If I have a simple "character" entity with a MeshInstanceRenderComponent and an associated mesh, how do I offset the mesh so that the base of the character is at the bottom of the mesh (the feet) rather than the center of the mesh?

    Outside of ECS, in pure gameObject terms, if I had a "character" gameObject and wanted its base position to be at its "feet", I would create an empty parent gameObject and a child gameObject with a mesh. Then I would just set the transform of the child gameObject so that it was moved "up" relative to the parent gameObject. I don't know the best approach for doing this in ECS.
     
  2. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    the best approach here is to edit mesh itself to have proper position
    option 2 is to move the entity the same way you do it with GameObjects
    there is an example of a hierarchy in samples repo
     
    rjohnson06 likes this.
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    ECS has hierarchy logic, with parent/child, you can do same as in default unity approach.
     
    rjohnson06 and rigidbuddy like this.
  4. rjohnson06

    rjohnson06

    Joined:
    May 27, 2017
    Posts:
    17
    Thanks for the replies, I ended up setting up the relationship entirely in code with the Attach component, I guess this is "pure" ECS

    Code (CSharp):
    1. var parent = entityManager.CreateEntity(Bootstrap.PeasantArchetype);
    2.             var child = entityManager.CreateEntity(typeof(Position), typeof(Scale));
    3.             var attach = entityManager.CreateEntity(typeof(Attach));
    4.  
    5.             entityManager.SetComponentData(parent, new Position { Value = spawnPosition });
    6.             entityManager.SetComponentData(parent, new MoveAgent(MoveAgentStatus.Idle, Bootstrap.Settings.enemySpeed, 0, 0, 0, 0, spawnPosition));
    7.  
    8.             entityManager.SetComponentData(child, new Position { Value = new float3(0, 1, 0) });
    9.             entityManager.SetComponentData(child, new Scale { Value = new float3(1, 10, 1) });
    10.             entityManager.AddSharedComponentData(child, Bootstrap.PeasantLook);
    11.  
    12.             entityManager.SetComponentData(attach, new Attach { Parent = parent, Child = child });
     
    T-Zee likes this.