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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    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,133
    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,133
    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,133