How to instantiate prefab and set the new entity's translation on the same frame?

Right now I have to wait one frame for the ECB to create the entity before setting its translation. The problem is that there will now be one frame where the resulting entity is at the default position (0,0,0) before being moved to the desired position. Is there a way to fix this problem

why can’t you just set the translation in the same ecb

The ECB would throw an exception. ECB.Instantiate works a little differently than ECB.CreateEntity - for the former, you’d have to wait one frame for the entity to be instantiated. If it helps the entity I’m trying to instantiate is a prefab

public Entity Instantiate(int jobIndex, Entity e)

Returns the entity as far as i’m aware?

I spawn entities using ecb.Instantiate and set the translation using ecb right after and it works just fine. It doesn’t throw an exception for me. However, the entity does always spawn at the origin and stays there for a frame until going to the new position. I’m sure there’s a way to fix that, but it seems really odd to me that it works like that by default.

You probably need to set the localtoworld.
translation/rotation etc are only used by transform system to create localtoworld which is what is actually used for rendering etc. If you use a buffer to set translation/rotation the localtoworld update is going to take a frame to be updated, hence spawning at origin.

2 Likes

I noticed that some entities would spawn at origin the move to where I had set their position and I had thought it was a frame later, but realised I was using the EndSimulationEntityCommandBufferSystem to instantiate the entity. This now means that the Translation component can’t be used to update the LocalToWorld component by the TransformSystem before the rendering system is run.

My fix was to move instantiation to the BeginInitializationEntityCommandBufferSystem which allows the transform system to correctly update the LocalToWorld matrix which is ultimately used by rendering.

4 Likes