1
\$\begingroup\$

I've started to learn DOTS some day ago, and decided to create a simple project. When I added some simple physics to the game I faced the problem. I added one object on the scene and another instantiated from a resource as a prefab (prefab contains physics shape and physics body), but they didn't collide and just go through. When I tried to place both objects in the scene they work perfectly. Then I tried to instantiate them both and got nothing, I mean they didn't collide... Maybe I miss something? I couldn't find any explanation for this in the Unity Physics doc.

Tried in Unity 2021.1 and 2020.3

Box on the scene configuration:

Game object on the scene

Sphere prefab configuration:
Sphere prfab

Spawn object code:

        var world = World.DefaultGameObjectInjectionWorld;
        var entityManager = world.EntityManager;

        var settings = GameObjectConversionSettings.FromWorld(world, null);
        _entityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(_playerPrefab, settings);

        Entity player = entityManager.Instantiate(_entityPrefab);
        entityManager.AddComponent<PlayerMovementComponent>(player);
        entityManager.SetComponentData(player, new PlayerMovementComponent()
        {
            Speed = 10f
        });
        entityManager.SetComponentData(player, new Translation()
        {
            Value = new float3(.5f,0f,.5f)
        });
\$\endgroup\$
2
  • \$\begingroup\$ Did you look at both entities in the entity debugger? Are there any obvious differences between them? \$\endgroup\$ Commented Jun 10, 2021 at 12:14
  • 1
    \$\begingroup\$ @Philipp thank you, looks like I found the problem, but I don't know how this happened... The entity from the scene has a physics collider component, but spawned hasn't. \$\endgroup\$ Commented Jun 10, 2021 at 12:34

1 Answer 1

2
\$\begingroup\$

Found the solution at the Unity's forums old thread, here's the link. Long story short I passed null as the second parameter when called GameObjectConversionSettings.FromWorld function. But you MUST pass any blob store, you can create a new one or you can get it from the world, by calling this script:

var conversionSystem = world.GetExistingSystem<GameObjectConversionSystem>();

So final spawn method should look like that:

    var world = World.DefaultGameObjectInjectionWorld;
    var entityManager = world.EntityManager;
    var blobStore = new BlobAssetStore();
    var settings = GameObjectConversionSettings.FromWorld(world, blobStore );
    _entityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(_playerPrefab, settings);
    Entity player = entityManager.Instantiate(_entityPrefab);

or like that if you want to use blobStore from the world:

    var world = World.DefaultGameObjectInjectionWorld;
    var entityManager = world.EntityManager;
    var conversionSystem = world.GetExistingSystem<GameObjectConversionSystem>();
    var settings = GameObjectConversionSettings.FromWorld(world, conversionSystem.BlobAssetStore);
    _entityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(_playerPrefab, settings);
    Entity player = entityManager.Instantiate(_entityPrefab);
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.