Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Set physics shape in script

Discussion in 'Physics for ECS' started by MadboyJames, Oct 14, 2019.

  1. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    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.
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    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:

    Code (CSharp):
    1.         BlobAssetReference<Collider> collider = BoxCollider.Create(new BoxGeometry
    2.         {
    3.             Center = float3.zero,
    4.             Orientation = quaternion.identity,
    5.             Size = new float3(1.0f, 1.0f, 1.0f),
    6.             BevelRadius = 0.05f
    7.         });
    8.         var colliderComponent = new PhysicsCollider { Value = collider };
    9.         entityManager.AddComponentData(entity, colliderComponent);
    10.  
    The BasePhysicsDemo.cs script (used by the Test scenes in the UnityPhysicsSamples) is a good starting point for code based creation of physics objects.