Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

[Solved] Instantiating a Netcode Ghost

Discussion in 'NetCode for ECS' started by XelasDev, Apr 3, 2020.

  1. XelasDev

    XelasDev

    Joined:
    Feb 20, 2016
    Posts:
    10
    Hi there,

    I'm slowly grasping all of these new concepts and need some advice. My goal is to create a system inside of the DOTS sample project that spawns in a ghost in response to a spawn request. Currently I place down a prefab in the scene which creates a spawn request during conversion. A component system spawns the correct ghost at the required position.

    What actually happens is that the ghost is created but only renders at world origin. On the server side the physics components are active and the prefab's translation shows the desired position.

    I've regenerated the collection code / snapshot code and the ghost prefab itself has all of the correct settings. Not sure where to go or what to look for from here. Is there anything wrong with my script?

    Thanks


    Code (CSharp):
    1.  
    2.  
    3. [UpdateInGroup(typeof(ServerSimulationSystemGroup))]
    4. public class GameEntityCubeServerSystem : ComponentSystem
    5. {
    6.     protected override void OnCreate()
    7.     {
    8.         RequireSingletonForUpdate<EnableSurvivalPrototypeGhostSendSystemComponent>();
    9.     }
    10.  
    11.     protected override void OnUpdate()
    12.     {
    13.             var prefabs = GetSingleton<GhostPrefabCollectionComponent>();
    14.             var serverPrefabs = EntityManager.GetBuffer<GhostPrefabBuffer>(prefabs.serverPrefabs);
    15.  
    16.             Entities.ForEach((Entity entity, ref GameEntityState state, ref GameEntitySpawnRequestData spawnRequest,
    17.                 ref Translation translation) =>
    18.             {
    19.                 Entity prefab;
    20.  
    21.                 switch((int)state.id)
    22.                 {
    23.                     case 0:
    24.                         prefab = serverPrefabs[SurvivalPrototypeGhostSerializerCollection.FindGhostType<CubeSnapshotData>()].Value;
    25.                         break;
    26.                     default:
    27.                         prefab = serverPrefabs[SurvivalPrototypeGhostSerializerCollection.FindGhostType<CubeSnapshotData>()].Value;
    28.                         break;
    29.                 }
    30.  
    31.                 var newState = state;
    32.  
    33.                 newState.entity = EntityManager.Instantiate(prefab);
    34.  
    35.                 EntityManager.RemoveComponent<GameEntitySpawnRequestData>(entity);
    36.                 EntityManager.SetComponentData<GameEntityState>(entity, newState);
    37.                 EntityManager.SetComponentData<Translation>(newState.entity, translation);
    38.             });
    39.     }
    40. }
    41.  
    42.  
     
    Last edited: Apr 3, 2020
  2. XelasDev

    XelasDev

    Joined:
    Feb 20, 2016
    Posts:
    10
  3. XelasDev

    XelasDev

    Joined:
    Feb 20, 2016
    Posts:
    10
    - Upgraded to Unity 2019.3.6f1
    - Cloned a fresh project
    - Created materials only using the provided shader graphs

    Still unable to solve this issue. The entity itself is interacting in the physics simulation but for some reason won't render anywhere else. Entity Debugger shows everything is fine. Any help would be greatly appreciated.
     
  4. ElliotB

    ElliotB

    Joined:
    Aug 11, 2013
    Posts:
    296
    If you use the entity manager to inspect the spawned ghost on both the server and the client, do they both have a non-zero translation component? Also, how does your Ghost prefab look like in the inspector, specifically the Ghost Authoring Component section?
     
  5. XelasDev

    XelasDev

    Joined:
    Feb 20, 2016
    Posts:
    10
    Hi there, thanks for responding.

    On the server world the entity's Translation, Rotation and LocalToWorld are all non-zero. The entity interacts with the physics environment as expected but nothing is being updated on the client aside from "GameEntityState".

    As for the Ghost prefab, the settings are here:

    .
     
    Last edited: Apr 5, 2020
  6. Jawsarn

    Jawsarn

    Joined:
    Jan 12, 2017
    Posts:
    261
    Hey, so have you checked that the Translation values on client and server matches?
     
  7. Jawsarn

    Jawsarn

    Joined:
    Jan 12, 2017
    Posts:
    261
    I noticed you're fetching for CubeSnapshotData, but are providing images of FrostySnapshotData?
     
  8. XelasDev

    XelasDev

    Joined:
    Feb 20, 2016
    Posts:
    10
    That's true. I've since fixed the typo with no change in outcome.

    I've updated my previous post to reflect the fact that Translation, Rotation, and LocalToWorld are not being updated in the client world.

    Update: On the Ghost prefab I set a manual field under "Translation" with the name of "Value" and the ghost now spawns at the correct position. Rotation and LocalToWorld are still not being updated.
     
    Last edited: Apr 5, 2020
  9. Jawsarn

    Jawsarn

    Joined:
    Jan 12, 2017
    Posts:
    261
    Ah, I see now. In your Ghost Authoring Component you're not telling it to sync Translation or Rotation, you can see this by it's not being bolded like your GameEntityState. It should by default, but not sure if you're using some example code that disables this by default, or have removed it yourself.
     
  10. Jawsarn

    Jawsarn

    Joined:
    Jan 12, 2017
    Posts:
    261
    upload_2020-4-5_17-37-53.png
     
    XelasDev likes this.
  11. ElliotB

    ElliotB

    Joined:
    Aug 11, 2013
    Posts:
    296
    It's also strange that the child components are not replicated - I thought that they were by default?
     
  12. XelasDev

    XelasDev

    Joined:
    Feb 20, 2016
    Posts:
    10
    Thank you so much for your help. The solution was setting manual fields to the values in your screenshot on both Translation and Rotation. For some reason I can't set the field on LocalToWorld, but it doesn't seem to matter. The server can now spawn entities that are properly replicated to clients.

    For reference I'm using NetCode 1-0.0.2 and Entities 17-0.5.0.

     
    Jawsarn likes this.