Search Unity

Resolved How get collider type out of PhysicsCollider component?

Discussion in 'Physics for ECS' started by Samsle, Sep 2, 2020.

  1. Samsle

    Samsle

    Joined:
    Mar 31, 2020
    Posts:
    110
    Hi,
    Imagine I have a prefab with a "PhysicsShape" component, with a defined "ShapeType", e.g. "Box".

    Code (CSharp):
    1. this.Entities.ForEach((
    2.     in PhysicsCollider physicsCollider
    3. ) => {
    4.     /* PhysicsCollider physicsCollider
    5.      *    BlobAssetReference<Collider> Value
    6.      *        Collider Value
    7.      *            ColliderType Type
    8.      */
    9.     Debug.Log(physicsCollider.Value.Value.Type.ToString());  // Output: 'Box'
    10. }
    How can I now access the properties of the BoxCollider? I guess I have somehow to cast it, but don't know how.

    BoxCollider boxCollider = (BoxCollider)physicsCollider.Value.Value

    -> Error: Cannot convert 'Unity.Physics.Collider' to 'Unity.Physics.BoxCollider'
     
    jmcusack likes this.
  2. Samsle

    Samsle

    Joined:
    Mar 31, 2020
    Posts:
    110
    Sorry for bothering you, found immediately after creation of the thread a solution:
    Code (CSharp):
    1. unsafe {
    2.     BoxCollider* boxColliderPtr = (BoxCollider*)physicsCollider.ColliderPtr;
    3.     Debug.Log(boxColliderPtr->Size.ToString()); // Output: float3(1f, 1f, 1f)
    4. }
    But maybe someone has an idea how to do it without unsafe context.
     
  3. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Yeah that's the way, you can't do it without unsafe code at the moment.
     
    Samsle likes this.
  4. Samsle

    Samsle

    Joined:
    Mar 31, 2020
    Posts:
    110
    ah ok, thanks for your reply!
     
    petarmHavok likes this.