Search Unity

Best way to get a car with a few child objects to display and move with the rest of the car in ECS?

Discussion in 'Entity Component System' started by MostHated, Feb 3, 2019.

  1. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    (Sorry, I didn't realize how long this would become until after it was all said and done, lol)

    Hello all,
    I decided to see if I could get my vehicle system from my game working with ECS so I created a new GameObject and added a GameObject Entity and RenderMesh component on it and added the body of the car and its material, as well as the other things I wanted like Position, Rotation, etc and just went on about my learning and testing and was able to get that into my test scene and get it to move so I wanted to move forward and see about getting the rest of the vehicle (windows, wheels, etc) along with it.

    I see that the RenderMesh Component had an option for submesh, I changed it to 1 to see what it would do (expected more options to appear for additional meshes) but nothing happened. Is that not what it is for? If not, what is the best way to add additional items to the vehicle? Originally they were just child objects of the main body of the car but I imagine that the GameObject I created as the basis for the vehicle probably doesn't work that way with ECS.

    Do I have to create a new GameObject and add a GameObject Entity, RenderMesh, Position, Rotation, etc to each component of the vehicle individually (such as each piece of glass, wheels, etc) and then make the movement job move each piece of the whole car individually but somehow make them all stay in the same position and rotation relative to the car? Opposed to how it would previously be done, where since the other items are children of the car body, they would maintain their local position and rotation relative to the vehicle if it were to move, turn, etc. If that is what needs to be done then that is what needs to be done, I am mostly wondering if that is, in fact, the case so I know how to proceed.

    I only started really looking into all of this yesterday/the night before, so there might be a good bit of bits and pieces I simply don't know exist or other ways of accomplishing this. If that is what needs to be done, the movement system I created uses a component on the body of the car called VehicleData which currently is only using a float3 in there I created called destinationPos so that the vehicle knows what direction to head as my original system I created uses an array of waypoints for the vehicle to navigate around the town. Would each piece of the car need that VehicleData component on it so that the movement system can move all of the pieces together toward the same destination?

    That sounds ok for a single vehicle, but I am going to have many, and the more I think about it the more of a disaster it will become as I am not sure how I would go about making sure each set of pieces of a vehicle moves along with the same vehicle. So there must be some way of keeping what would be considered child objects grouped or part of a parent in some way? The only parts of the vehicle that would technically be doing anything different from the rest of it would be the wheels and in its current form I simply have a small script on them that has a rotation and I just adjust the speed and direction of the spin, they don't turn left or right or anything, just spin in the proper direction for which they are facing.

    I do apologize that this ended up becoming so long. As I was typing it out it just made me realize there was a lot more to my initial question that I am just not sure about. As I mentioned there may be an easier and better way to go about getting this to do what I am needing it to do, so I am just hoping that someone might be able to point me in the right direction!

    TLDR - I need to make my vehicle which is currently just a body using GameObject Entity that gets moved around by a job have the rest of the parts (hood, windows, wheels) display and move with the car since I could not just do it with the completed prefab that originally made up the vehicle. What is the best way to go about it, or where might I be able to find some current information on what this would require?

    Thanks!
    -MH
     
  2. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    If I read you correctly, you're probably looking for the Attach component.
    The setup is much the same as with GameObjects. You attach all your carpart entities to the car and then just move the car.
    The process for attaching them is a little bit different though. You create a new Attach entity with the parent and child entities and then ecs will take care of the rest. There's code example in the link.
     
  3. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    That certainly seems like it would be what I am after. I will give it a go and see what happens. Much appreciated!

    Looking over what it says, I would assume that I would still need to make each part of the car a separate gameobject entity with a rendermesh still before anything else and then I would be able to attach it and have it follow the parents transform, and the initial values such as:

    Code (CSharp):
    1.  
    2.  m_Manager.SetComponentData(child, new Position {Value = new float3(0, 0, 1)});
    3.  
    would probably be its position in which it would be and stay relative to the parent?
     
    Last edited: Feb 3, 2019
  4. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Yes every part needs to be a separate Entity, just like with GameObjects.
    Yeah I think that's the case, and not just for the initial position but for as long as it's attached because to get the world position, you have to go:
    Code (CSharp):
    1. var childWorldPosition = m_Manager.GetComponentData<LocalToWorld>(child).Value.c3;
    I haven't used Attach myself so just going off the docs. Someone will correct if I'm wrong.
     
  5. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    Definitely makes sense and gets started in the right direction at least so I know the names of some things to look up. Thanks again. : D