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! ![]()
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}");