Search Unity

get transform of ColliderCastHit?

Discussion in 'Physics for ECS' started by Davood_Kharmanzar, Feb 9, 2020.

  1. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    411
    hello,
    how can i get the transform of ColliderCastHit?
    and howto check if its not null?

    P.S:
    this is my code to get hit:

    Code (CSharp):
    1.        
    2.         [BurstCompile]
    3.         private unsafe bool SphereCast(out ColliderCastHit result)
    4.         {
    5.             BlobAssetReference<Unity.Physics.Collider> Collider = Unity.Physics.SphereCollider.Create(new SphereGeometry() { Center = ZombieNPC.transform.position, Radius = Range }, new CollisionFilter() { BelongsTo = (uint)targetMask.value, CollidesWith = (uint)obstacleMask.value });
    6.             return new CollisionWorld().CastCollider(new ColliderCastInput() { Collider = (Unity.Physics.Collider*)Collider.GetUnsafePtr(), Orientation = quaternion.identity, Start = ZombieNPC.transform.position }, out result);
    7.         }
    8.  
     
    Last edited: Feb 9, 2020
  2. papop

    papop

    Joined:
    Oct 16, 2019
    Posts:
    1
    Hey,
    If CollisionWorld.CastCollider() returns false, the hit didn't happen, and therefore you shouldn't check for transform.
    In ColliderCastHit struct, there is rigid body index of the body that was hit. You can use that to get transform of that body.
    For example:

    if (SphereCast (out ColliderCastHit result))
    {
    var transform = CollisionWorld.Bodies[result.RigidBodyIndex].WorldFromBody;
    }
     
    Last edited: Feb 14, 2020