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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

get transform of ColliderCastHit?

Discussion in 'DOTS Physics' 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
unityunity