Set physics shape in script

I have an archetype that needs to be able to receive raycasts. The renderMesh for this archetype is a small quad, so I’m trying to get a collision box to cover it. I don’t have much specifics about my process because I just couldn’t find anything on how to do this. I did notice that PhysicsShape is a class and not an IComponentData, so I don’t think I can use it in a SetComponentData method. Do I have to convert a gameobject to an entity to get the collision mesh?

Any help is appreciated.

PhysicsShape is a design time component for use in the editor. As part of the Unity Physics conversion system the data in the PhysicsShape is used to create a number of IComponentDatas. The specific IComponentData you are looking for is PhysicsCollider. You can create a BoxCollider for example as follows:

        BlobAssetReference<Collider> collider = BoxCollider.Create(new BoxGeometry
        {
            Center = float3.zero,
            Orientation = quaternion.identity,
            Size = new float3(1.0f, 1.0f, 1.0f),
            BevelRadius = 0.05f
        });
        var colliderComponent = new PhysicsCollider { Value = collider };
        entityManager.AddComponentData(entity, colliderComponent);

The BasePhysicsDemo.cs script (used by the Test scenes in the UnityPhysicsSamples) is a good starting point for code based creation of physics objects.

4 Likes