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.

Question Incorrect operation of Raycast

Discussion in 'Physics for ECS' started by MrKsi, Sep 7, 2022.

  1. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    123
    Greetings, I ran into such a problem, the fact is that I use the standard Raycast function proposed by Unity here is the code:

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Mathematics;
    3. using Unity.Physics;
    4.  
    5. public static class ECSUtilits
    6. {
    7.     public static Entity Raycast(float3 fromPosition,float3 toPosition)
    8.     {
    9.         var buildPhysicsWord = DOTSComponents.Build;
    10.         var collisiionWorld = buildPhysicsWord.PhysicsWorld.CollisionWorld;
    11.  
    12.         var raycastInput = new RaycastInput
    13.         {
    14.             Start = fromPosition,
    15.             End = toPosition,
    16.             Filter = new CollisionFilter
    17.             {
    18.                 BelongsTo = ~0u,
    19.                 CollidesWith = ~0u,
    20.                 GroupIndex = 0,
    21.             }
    22.         };
    23.  
    24.         var raycastHit = new RaycastHit();
    25.  
    26.         if(collisiionWorld.CastRay(raycastInput,out raycastHit))
    27.         {
    28.             var hitEntity = buildPhysicsWord.PhysicsWorld.Bodies[raycastHit.RigidBodyIndex].Entity;
    29.             return hitEntity;
    30.         }
    31.         else
    32.         {
    33.             return Entity.Null;
    34.         }
    35.     }
    36.  
    37. }
    And when this ray hits an object, it processes the wrong object here:

    Code (CSharp):
    1.   if (Input.GetMouseButton(0))
    2.         {
    3.             var ray = new Ray(transform.position, transform.forward);
    4.             var entity = ECSUtilits.Raycast(ray.origin, ray.direction * DistanceShoot);
    5.             if(entity != Entity.Null)
    6.             {
    7.                 try
    8.                 {
    9.                     var fragment = _entityManager.GetComponentData<FragmentECS>(entity);
    10.                     fragment.Direction = Cam.transform.forward;
    11.                     fragment.Speed = 2;
    12.                     fragment.IsDelected = true;
    13.                     _entityManager.SetComponentData(entity, fragment);
    14.                 }
    15.                 catch
    16.                 {
    17.  
    18.                 }
    19.             }
    20.         }
    I shoot from the camera forward, according to Debug.LogRay everything works as it should, but for some reason the beam itself processes incorrect objects, how to get exactly the object that the beam hit? Where could I have made a mistake. Help please.
     
  2. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    123
    Here's what's happening so far:
    Here's what it turns out so far, I don't even get a beam from the camera into the object, but for some reason he still thinks that I hit him
     

    Attached Files:

    • 10.gif
      10.gif
      File size:
      2.2 MB
      Views:
      76
  3. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    153
    You neglected to add ray.origin for the argument passed as toPosition.
    Code (CSharp):
    1. var entity = ECSUtilits.Raycast(ray.origin, ray.origin + ray.direction * DistanceShoot);
     
    MrKsi likes this.
  4. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    123
    Here they are devoted DOTS fans, thanks a lot to you dude_rtfm!