Incorrect operation of Raycast

Greetings, I ran into such a problem, the fact is that I use the standard Raycast function proposed by Unity here is the code:

using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;

public static class ECSUtilits
{
    public static Entity Raycast(float3 fromPosition,float3 toPosition)
    {
        var buildPhysicsWord = DOTSComponents.Build;
        var collisiionWorld = buildPhysicsWord.PhysicsWorld.CollisionWorld;

        var raycastInput = new RaycastInput
        {
            Start = fromPosition,
            End = toPosition,
            Filter = new CollisionFilter
            {
                BelongsTo = ~0u,
                CollidesWith = ~0u,
                GroupIndex = 0,
            }
        };

        var raycastHit = new RaycastHit();

        if(collisiionWorld.CastRay(raycastInput,out raycastHit))
        {
            var hitEntity = buildPhysicsWord.PhysicsWorld.Bodies[raycastHit.RigidBodyIndex].Entity;
            return hitEntity;
        }
        else
        {
            return Entity.Null;
        }
    }

}

And when this ray hits an object, it processes the wrong object here:

  if (Input.GetMouseButton(0))
        {
            var ray = new Ray(transform.position, transform.forward);
            var entity = ECSUtilits.Raycast(ray.origin, ray.direction * DistanceShoot);
            if(entity != Entity.Null)
            {
                try
                {
                    var fragment = _entityManager.GetComponentData<FragmentECS>(entity);
                    fragment.Direction = Cam.transform.forward;
                    fragment.Speed = 2;
                    fragment.IsDelected = true;
                    _entityManager.SetComponentData(entity, fragment);
                }
                catch
                {

                }
            }
        }

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.

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

8421750--1114326--10.gif

You neglected to add ray.origin for the argument passed as toPosition.

var entity = ECSUtilits.Raycast(ray.origin, ray.origin + ray.direction * DistanceShoot);
1 Like

Here they are devoted DOTS fans, thanks a lot to you dude_rtfm!