[Solved] Instantiating a Netcode Ghost

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

[UpdateInGroup(typeof(ServerSimulationSystemGroup))]
public class GameEntityCubeServerSystem : ComponentSystem
{
    protected override void OnCreate()
    {
        RequireSingletonForUpdate<EnableSurvivalPrototypeGhostSendSystemComponent>();
    }

    protected override void OnUpdate()
    {
            var prefabs = GetSingleton<GhostPrefabCollectionComponent>();
            var serverPrefabs = EntityManager.GetBuffer<GhostPrefabBuffer>(prefabs.serverPrefabs);

            Entities.ForEach((Entity entity, ref GameEntityState state, ref GameEntitySpawnRequestData spawnRequest,
                ref Translation translation) =>
            {
                Entity prefab;

                switch((int)state.id)
                {
                    case 0:
                        prefab = serverPrefabs[SurvivalPrototypeGhostSerializerCollection.FindGhostType<CubeSnapshotData>()].Value;
                        break;
                    default:
                        prefab = serverPrefabs[SurvivalPrototypeGhostSerializerCollection.FindGhostType<CubeSnapshotData>()].Value;
                        break;
                }

                var newState = state; 

                newState.entity = EntityManager.Instantiate(prefab);

                EntityManager.RemoveComponent<GameEntitySpawnRequestData>(entity);
                EntityManager.SetComponentData<GameEntityState>(entity, newState);
                EntityManager.SetComponentData<Translation>(newState.entity, translation);
            });
    }
}

Update: might be a renderer issue. The manual says that an incorrect material or shader setup could make your mesh render at 0,0,0 and no logs or errors would appear. Modifying my shader graphs hasn’t fixed the issue. Currently double-checking everything.

https://docs.unity3d.com/Packages/com.unity.rendering.hybrid@0.4/manual/index.html

  • 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.

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?

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:

.

Hey, so have you checked that the Translation values on client and server matches?

I noticed you’re fetching for CubeSnapshotData, but are providing images of FrostySnapshotData?

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.

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.

5673334--591406--upload_2020-4-5_17-37-53.png

1 Like

It’s also strange that the child components are not replicated - I thought that they were by default?

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.

1 Like