How to check Ray perception Sensor 3D?

I want to know what value is sent by each Ray Perception. What should I do?

bumping this, I am also struggling alot trying to access each of the values by the ray perceptions. I’ll be posting here if i find out myself, but would greatly like some help accessing these values myself.

To my understanding you need to do something like this:

public RayPerceptionSensor agentRaycastSensor;
OR
public RayPerceptionSensorComponent3D agentRaycastSensor;

You then need to access the gameObject with something like this>
agentRaycastSensor = transform.Find(“AgentRaycastSensor”).GetComponent();

I asked chatGPT to write me a function that could help, but it doesn’t work since the toolkit has been updated i think. The code goes

private Transform GetFirstDetectedObjectWithTag(RayPerceptionSensorComponent3D sensorComponent, string tag)
{
RayPerceptionInput rayPerceptionInput = new RayPerceptionInput();
sensorComponent.GetRayPerceptionInput(ref rayPerceptionInput);
RayPerceptionOutput rayPerceptionOutput = new RayPerceptionOutput();
sensorComponent.RaySensor.Perceive(rayPerceptionInput, ref rayPerceptionOutput);
for (int i = 0; i < rayPerceptionOutput.RayOutputs.Count; i++)
{
var rayOutput = rayPerceptionOutput.RayOutputs*;*
if (rayOutput.HitTaggedObject && rayOutput.HitTag == tag)
{
return rayOutput.HitObject.transform;
}
}
return null;
}
IDE goes mad because stuff like rayOutput.HitTag doesn’t exist anymore.
I’ll hit you up if i find out more, hopefully someone who knows more about this than me can help, i’m just tired of seeing this forum go without replies

You can check the values from Ray Perception Sensor like below:

using Unity.MLAgents.Sensors;

private void checkRayCast()
{
        RayPerceptionSensorComponent3D m_rayPerceptionSensorComponent3D = GetComponent<RayPerceptionSensorComponent3D>();

        var rayOutputs = RayPerceptionSensor.Perceive(m_rayPerceptionSensorComponent3D.GetRayPerceptionInput()).RayOutputs;
        int lengthOfRayOutputs = rayOutputs.Length;

        // Alternating Ray Order: it gives an order of
        // (0, -delta, delta, -2delta, 2delta, ..., -ndelta, ndelta)
        // index 0 indicates the center of raycasts
        for (int i = 0; i < lengthOfRayOutputs; i++)
        {
            GameObject goHit = rayOutputs[i].HitGameObject;
            if (goHit != null)
            {
                var rayDirection = rayOutputs[i].EndPositionWorld - rayOutputs[i].StartPositionWorld;
                var scaledRayLength = rayDirection.magnitude;
                float rayHitDistance = rayOutputs[i].HitFraction * scaledRayLength;

                // Print info:
                string dispStr = "";
                dispStr = dispStr + "__RayPerceptionSensor - HitInfo__:\r\n";
                dispStr = dispStr + "GameObject name: " + goHit.name + "\r\n";
                dispStr = dispStr + "Hit distance of Ray: " + rayHitDistance + "\r\n";
                dispStr = dispStr + "GameObject tag: " + goHit.tag + "\r\n";
                Debug.Log(dispStr);
            }
        }
}

Hope this helps.

3 Likes

thank you so much it worked