Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Raycast and collision with entities without rigidbodies

Discussion in 'Physics for ECS' started by sstrong, Dec 10, 2019.

  1. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,259
    I have a parallel job in which I'd like to detect which Entities I hit. Not all entities will have rigidbodies so the following isn't so useful.

    Code (CSharp):
    1. Entity e = physicsWorldSystem.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity
    My job looks something like:
    Code (CSharp):
    1.         [BurstCompile]
    2.         struct ProjectilePhysicsRayJob : IJobForEach<Translation, Projectile>
    3.         {
    4.             public NativeArray<Unity.Physics.RaycastHit> raycastResultsInJob;
    5.             [ReadOnly] public Unity.Physics.CollisionWorld collisionWorldInJob;
    6.             public float deltaTime;
    7.  
    8.             public void Execute([ReadOnly] ref Translation position, ref Projectile projectile)
    9.             {
    10.                 Unity.Physics.RaycastInput raycastInput = new Unity.Physics.RaycastInput
    11.                 {
    12.                     Start = position.Value,                  
    13.                     End = position.Value + (projectile.velocity * deltaTime),
    14.                     Filter = Unity.Physics.CollisionFilter.Default
    15.                 };
    16.  
    17.                 if (collisionWorldInJob.CastRay(raycastInput, out Unity.Physics.RaycastHit hit))
    18.                 {
    19.                     raycastResultsInJob[projectile._tempIdx] = hit;
    20.                 }
    21.             }
    22.         }
    How can I determine which Entities and/or meshes where hit?
     
  2. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    70
    Unity.Physics raycasts can only hit entities which have a PhysicsCollider component (a.k.a. becomes a rigid body). They include an index of that rigid body in the hit. The bodies stores the entity it was created from. So in your example just do:

    Code (csharp):
    1. Entity hitEntity = collisionWorldInJob.Bodies[hit.RigidBodyIndex].Entity;
     
    ekakiya likes this.
  3. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,259
    So, they do have a PhysicsCollider which gets added automatically from the Collider on the parent gameobject when ConvertGameObjectHierarchy(prefab.gameObject, conversionSettings) is called followed by
    Code (CSharp):
    1.  Entity entity = entityManager.Instantiate(prefabEntity);
    upload_2019-12-10_19-37-9.png

    If I create a cube in scene attach a ConvertToEntity and PhysicsShape (no rigidbody), Unity.Physics Raycast works fine. Just not when I instantiate in code.
     
  4. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
  5. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,259