#Entities 0.51 Raycasting

1 messages · Page 1 of 1 (latest)

median estuary
#

I’m using Entities 0.51 and I’m trying to get a simple raycast to work with ECS. The Unity.Physics ray is hitting the desired entity, but the ray is ignoring the ray distance value. Even if I put the origin of the ray 2000 units into the sky and set the ray distance to 1, it still hits the ground! I have a UnityEngine ray debugged to visualize where the ray should start and end and it’s in the right spot. Any ideas?

leaden stirrup
#

i'm not sure i remember this right. i've to check later but i think length is ignored now and has to be checked with your code. could be wrong though.

median estuary
#

Ahhh I see. That would do it. So I have to check if the hit.fraction is below a set value?

leaden stirrup
#

no fraction is something else. (normalized length)

#

how do you setup the ray? it requires start and end

median estuary
#

Oh I thought I read in the docs that the fraction was essentially the distance to the hit.

leaden stirrup
#
        ///
        /// <value> Returns a value between 0 and 1. </value>
        public float Fraction { get; set; }```
median estuary
#

Ohhh okay. I can post screenshots and the relevant code in a second.

leaden stirrup
#

i've never used it. i saw some code that uses it to get hits inside the collider or smth.

#

Lower level ray cast routines against primitives have a slightly different input. They do not compute the surface intersection point explicitly for efficiency reasons. Instead, given a ray of origin - O and displacement (combining direction & distance) - D, they return a hit fraction - f, which can be used later to compute the hit position if needed using O + (D * f). See Ray in API documentation for more details.

    Note: Rays starting inside primitives (spheres, capsules, box and convex shapes) confirm a hit at the starting point, but do not report another intersection as the ray leaves the volume.
#

oh there are explicit distance queries

#

and often a bit confusing 😄

median estuary
#

Okay, this seems to work for me

public partial class RayCastTest : SystemBase
{
    Unity.Physics.RaycastHit hit;

    public bool Raycast(float3 RayFrom, float3 RayTo)
    {
        var physicsWorldSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystem<Unity.Physics.Systems.BuildPhysicsWorld>();
        var collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld;
        RaycastInput input = new RaycastInput()
        {
            Start = RayFrom,
            End = RayTo,
            Filter = new CollisionFilter()
            {
                BelongsTo = ~0u,
                CollidesWith = ~0u,
                GroupIndex = 0
            }
        };

        hit = new Unity.Physics.RaycastHit();
        bool hasHit = collisionWorld.CastRay(input, out hit);
        if (hasHit) {
            return true;
        } else {
            return false;
        }
    }

    protected override void OnUpdate() {
        Entities.ForEach((in SuspensionTag suspensionTag, in LocalToWorld ltw, in Translation translation) => {
            float rayDistance = 2f;
            UnityEngine.Ray ray = new UnityEngine.Ray(new Vector3(ltw.Value.c3.x,
                ltw.Value.c3.y, ltw.Value.c3.z), -ltw.Up * rayDistance);
            if (Raycast(ray.origin, ray.direction * rayDistance)) {
                Debug.Log("True, Hit Distance: " + (ray.origin + ((ray.direction * rayDistance) * hit.Fraction)));
            } else {
                Debug.Log("False");
            }
            Debug.DrawRay(ray.origin, ray.direction * rayDistance, Color.cyan);
        }).WithoutBurst().Run();
    }
}