Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback How to CastRay on SphereCollider struct without Physics.SphereCollider.Create allocation

Discussion in 'Physics Previews' started by andrew-lukasik, Oct 7, 2020.

  1. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    249
    Hi,
    I'm trying to CastRay() on SphereCollider struct without Physics.SphereCollider.Create allocation. But it doesn't work and wondering what am I missing here?

    Code (CSharp):
    1. using Physics = Unity.Physics;
    2.  
    3. bool TraceRaySphere
    4. (
    5.     float3 spherePos ,
    6.     float sphereRadius ,
    7.     float3 rayStart ,
    8.     float3 rayEnd ,
    9.     out Physics.RaycastHit raycastHit
    10. )
    11. {
    12.     var ray = new Physics.RaycastInput{
    13.         Start   = rayStart ,
    14.         End     = rayEnd ,
    15.         Filter  = Physics.CollisionFilter.Default
    16.     };
    17.     var geometry  = new Physics.SphereGeometry{
    18.         Center  = spherePos ,
    19.         Radius  = sphereRadius
    20.     };
    21.     var sphere = new Physics.SphereCollider{
    22.         Geometry    = geometry ,
    23.         Filter      = Physics.CollisionFilter.Default
    24.     };
    25.     return sphere.CastRay( ray , out raycastHit );
    26. }
    Code (CSharp):
    1. using Physics = Unity.Physics;
    2.  
    3. bool TraceRaySphere
    4. (
    5.     float3 spherePos ,
    6.     float sphereRadius ,
    7.     float3 rayStart ,
    8.     float3 rayEnd ,
    9.     out Physics.RaycastHit raycastHit
    10. )
    11. {
    12.     var ray = new Physics.RaycastInput{
    13.         Start   = rayStart ,
    14.         End     = rayEnd ,
    15.         Filter  = Physics.CollisionFilter.Default
    16.     };
    17.     var geometry  = new Physics.SphereGeometry{
    18.         Center  = spherePos ,
    19.         Radius  = sphereRadius
    20.     };
    21.     var sphereBlobAsset = Physics.SphereCollider.Create( geometry );
    22.     bool didHit = sphereBlobAsset.Value.CastRay( ray , out raycastHit );
    23.     sphereBlobAsset.Dispose();
    24.     return didHit;
    25. }
    26.  

    My goal here is to raycast a theoretical shape without heap being involved.
     
    Last edited: Oct 7, 2020
    AlanMattano likes this.
  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    You'd need to call the Init() method of the collider to have it populate all the fields. Unfortunately, that thing is private. You can try making it public to see if it works and we can think about exposing that as the public method of creating colliders.

    Edit: something like
    var collider = default(SphereCollider);
    collider.Init(geometry, filter, material);
     
    andrew-lukasik likes this.
  3. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    249
    I can confirm that calling this private `Init` solves the issue indeed!

    Please consider making this method public. This may fill the long-standing gap for these basic, allocation-free physics calculations in Unity.

    My practical use case example here is 3d UI built with ecs/dots where I need raycast calculations but don't want to allocate additional colliders.