you need a reference to your perception ray (can pass this like a normal reference or get it with GetComponent etc)
i.e.
[SerializeField]
RayPerceptionSensorComponent2D rayPerceptionSensor;
then in your CollectObservations function you can call something like
// Get the ray perception output from the sensor
RayPerceptionOutput rayPerceptionOutput = rayPerceptionSensor.RaySensor.RayPerceptionOutput;
var rayOutputs = rayPerceptionOutput.RayOutputs;
for (int i = 0; i < rayOutputs.Length; i++)
{
Debug.Log($"ray {i} hit something ? {rayOutputs[i].HasHit}");
if (rayOutputs[i].HasHit)
{
Debug.Log($"hit gameobject = {rayOutputs[i].HitGameObject.name}");
}
}
so to get the info you asked for you can do something like this
// Get the ray perception output from the sensor
RayPerceptionOutput rayPerceptionOutput = rayPerceptionSensor.RaySensor.RayPerceptionOutput;
RayPerceptionOutput.RayOutput[] rayOutputs = rayPerceptionOutput.RayOutputs;
for (int i = 0; i < rayOutputs.Length; i++)
{
Debug.Log($"ray {i} hit something ? {rayOutputs[i].HasHit}");
if (rayOutputs[i].HasHit)
{
Debug.Log($"hit gameobject = {rayOutputs[i].HitGameObject.name}");
Debug.Log($"hit distance = {rayOutputs[i].HitFraction}");
Rigidbody2D hitRB = rayOutputs[i].HitGameObject.GetComponentInParent<Rigidbody2D>();
if (hitRB)
{
Debug.Log($"hit velo = {hitRB.velocity}");
Debug.Log($"hit weight = {hitRB.mass}");
}
}
}
(should just be a case of swapping the relevant commands to 3D if required)