Help: Raycasts don't collide with objects in the scene

Hi everyone,

I’m currently using RaycastCommand to simulate a LiDAR, but I’m running into an issue where none of the rays are registering hits. In my scene, I have cones as objects, each with a mesh collider, and they’re all set to the default layer mask. When I visualize the rays, I can see them passing right through the cones, but unfortunately, no raycastHit is being detected.

As a side note, when I switch to using Physics.Raycast(…) instead of new RaycastCommand(…), everything works perfectly, and hits are detected just fine. However, for my project, I need to use job scheduling…

If anyone has any insights into why this might be happening, I’d really appreciate the help!

Thanks so much in advance! :slight_smile:

using Unity.Collections;
using Unity.Jobs;
using UnityEngine;

public class LiDAR360Batch : MonoBehaviour
{
    public int verticalRes = 64;
    public int horizontalRes = 2048;
    public float verticalFOV = 33f; // degrees
    public float maxDistance = 100f; // meters
    public float frequency = 10f; // Hz
    private NativeArray<RaycastHit> results;
    private NativeArray<RaycastCommand> commands;
    private LayerMask layerMask = Physics.DefaultRaycastLayers; 

    private float timer = 0f;
    private float horizontalAngleStep;
    private float verticalAngleStep;

    private void Start()
    {
        int totalRays = verticalRes * horizontalRes;
        results = new NativeArray<RaycastHit>(totalRays, Allocator.Persistent);
        commands = new NativeArray<RaycastCommand>(totalRays, Allocator.Persistent);

        horizontalAngleStep = 360f / horizontalRes;
        verticalAngleStep = verticalFOV / verticalRes;
    }

    private void OnDestroy()
    {
        if (results.IsCreated) results.Dispose();
        if (commands.IsCreated) commands.Dispose();
    }

    private void Update()
    {
        timer += Time.deltaTime;
        if (timer >= 1f / frequency)
        {
            timer -= 1f / frequency;
            PerformLiDARScan();
        }
    }

    private void PerformLiDARScan()
    {
        Vector3 origin = transform.position;

        for (int h = 0; h < horizontalRes; h++)
        {
            float horizontalAngle = h * horizontalAngleStep;
            for (int v = 0; v < verticalRes; v++)
            {
                float verticalAngle = (v * verticalAngleStep) - (verticalFOV / 2f);
                Quaternion rotation = Quaternion.Euler(verticalAngle, horizontalAngle, 0);
                Vector3 direction = rotation * Vector3.forward;
                int index = h * verticalRes + v;

                QueryParameters queryParameters = new QueryParameters(
                    layerMask,
                    false, // hitMultipleFaces: stop at the first object hit
                    QueryTriggerInteraction.Collide, // Ensure trigger colliders are hit
                    true // hitBackfaces
                );
                commands[index] = new RaycastCommand(origin, direction, queryParameters, maxDistance);
            }
        }

        // Schedule raycasts
        int batchSize = 32;
        int innerloopBatchCount = 64;
        JobHandle handle = RaycastCommand.ScheduleBatch(commands, results, 1, innerloopBatchCount);
        handle.Complete();
        Debug.Log("Raycast Batch Completed.");

        // Process results
        int hitCount = 0;
        for (int i = 0; i < results.Length; i++)
        {
            Debug.Log($"{results[i].point}");
            if (results[i].collider != null)
            {
                hitCount++;
                //process hit information
                //point cloud
            }
        }

        Debug.Log($"LiDAR Scan Complete. Total Hits: {hitCount}");

I’m not sure if it’s the problem, but this is probably wrong:

The function you’re calling here is this one:

public static unsafe JobHandle ScheduleBatch(
      NativeArray<RaycastCommand> commands,
      NativeArray<RaycastHit> results,
      int minCommandsPerJob,
      int maxHits,
      JobHandle dependsOn = default (JobHandle))
    {

You’re sending a variable you have named “innerloopBatchCount” to a parameter named “maxHits”. That being said, that should cause you to get 1 hit, not 0 hits, so if you’re getting 0 that’s probably not the problem?

Oops, my bad! Thanks for pointing that out. Must’ve changed it while debugging. I changed it back to my batchSize variable, but unfortunately, it’s still not working.