1
\$\begingroup\$

I am implementing a flight dynamics model using Unity's built-in ECS package, Entities, and I keep running into one particular issue where the aircraft I'm trying to spawn gets converted into an entity twice.

Essentially, I have a singleton Monobehaviour that handles setting up the aircraft entity from a GameObject Prefab. Here's the script:

public class GameManager : MonoBehaviour
{

    private BlobAssetStore blobAssetStore;

    public static GameManager main;

    public GameObject planePrefab;

    public float zBound = 0.0f;
    public float throttleMaxBound = 1.0f;
    public float alphaMaxBound = 20.0f;
    public float bankMaxBound = 20.0f;
    public float flapMaxBound = 40.0f;
    public float throttleMinBound = 0.0f;
    public float alphaMinBound = 0.0f;
    public float bankMinBound = -20.0f;
    public float flapMinBound = 0.0f;

    public State state;
    public Properties prop;

    Entity planeEntityPrefab;
    EntityManager manager;

    private void Awake()
    {
        if (main != null && main != this)
        {
            Destroy(gameObject);
            return;
        }

        main = this;

        manager = World.DefaultGameObjectInjectionWorld.EntityManager;

        blobAssetStore = new BlobAssetStore();

        GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, blobAssetStore);
        planeEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(planePrefab, settings);
        

        SpawnEntity();
    }

    private void OnDestroy()
    {
        blobAssetStore.Dispose();
    }

    void SpawnEntity()
    {
        Entity plane = manager.Instantiate(planeEntityPrefab);

        prop = new Properties(
            16.2f,
            10.9f,
            2.0f,
            0.0889f,        // slope of Cl-alpha curve
            0.178f,         // intercept of Cl-alpha curve
            -0.1f,          // post-stall slope of Cl-alpha curve
            3.2f,           // post-stall intercept of Cl-alpha curve
            16.0f,          // alpha when Cl=Clmax
            0.034f,         // parasite drag coefficient
            0.77f,          // induced drag efficiency coefficient
            1114.0f,
            119310.0f,
            40.0f,          // revolutions per second
            1.905f,
            1.83f,          //  propeller efficiency coefficient
            -1.32f          //  propeller efficiency coefficient
            );

        state = new State(
            0.0f,           // time
            0.0f,           // ODE results
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f,           // roll angle
            4.0f,           // pitch angle
            0.0f,           // throttle percentage
            0.0f            // flap deflection
            );

        float3 position = new float3(state.q1, state.q3, state.q5);

        manager.SetComponentData(plane, new Translation { Value = position });
    }

}

After a little bit of commenting here and there, I was able to determine that the following two lines are responsible for creating two separate entities, though the latter is the only one I'm capable of manipulating using the system. It's also worth mentioning that these two entities are virtually identical in the Entity Debugger, except for the fact that the first entity contains the "prefab" tag.

planeEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(planePrefab, settings);

and...

Entity plane = manager.Instantiate(planeEntityPrefab);

Is there a way to spawn this entity without both showing up in the debugger or is this unavoidable? If not, is there a way to remove the entity with the "prefab" tag from the world?

Thanks! :)

\$\endgroup\$
0
0
\$\begingroup\$

Turns out the solution was more simple than I expected it to be.

To remove the prefab, I simply included this line after instantiating the prefab in the SpawnEntity method:

manager.DestroyEntity(planeEntityPrefab);

I needed the planeEntityPrefab variable in order to instantiate the aircraft, but after the following line, it seemed safe for me to delete the prefab. The entire GameManager script now looks as follows:

public class GameManager : MonoBehaviour
{
    private BlobAssetStore blobAssetStore;

    public static GameManager main;

    public GameObject planePrefab;

    public float zBound = 0.0f;
    public float throttleMaxBound = 1.0f;
    public float alphaMaxBound = 20.0f;
    public float bankMaxBound = 20.0f;
    public float flapMaxBound = 40.0f;
    public float throttleMinBound = 0.0f;
    public float alphaMinBound = 0.0f;
    public float bankMinBound = -20.0f;
    public float flapMinBound = 0.0f;

    public State state;
    public Properties prop;

    Entity planeEntityPrefab;
    EntityManager manager;

    private void Awake()
    {
        if (main != null && main != this)
        {
            Destroy(gameObject);
            return;
        }

        main = this;

        manager = World.DefaultGameObjectInjectionWorld.EntityManager;

        blobAssetStore = new BlobAssetStore();

        GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, blobAssetStore);
        planeEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(planePrefab, settings);

        SpawnEntity();
    }

    private void OnDestroy()
    {
        blobAssetStore.Dispose();
    }

    void SpawnEntity()
    {
        Entity plane = manager.Instantiate(planeEntityPrefab);

        manager.DestroyEntity(planeEntityPrefab);

        prop = new Properties(
            16.2f,
            10.9f,
            2.0f,
            0.0889f,        // slope of Cl-alpha curve
            0.178f,         // intercept of Cl-alpha curve
            -0.1f,          // post-stall slope of Cl-alpha curve
            3.2f,           // post-stall intercept of Cl-alpha curve
            16.0f,          // alpha when Cl=Clmax
            0.034f,         // parasite drag coefficient
            0.77f,          // induced drag efficiency coefficient
            1114.0f,
            119310.0f,
            40.0f,          // revolutions per second
            1.905f,
            1.83f,          //  propeller efficiency coefficient
            -1.32f          //  propeller efficiency coefficient
            );

        state = new State(
            0.0f,           // time
            0.0f,           // ODE results
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f,           // roll angle
            4.0f,           // pitch angle
            0.0f,           // throttle percentage
            0.0f            // flap deflection
            );

        float3 position = new float3(state.q1, state.q3, state.q5);

        manager.SetComponentData(plane, new Translation { Value = position });
    }

}

When I run the game, the entity debugger only shows the instantiated aircraft and world time entities. Problem solved!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.