How to get values from multiple Ray Perception Sensor 3Ds?

Hi.

I use three Ray Perception Sensor Components for measuring distances from the agent to some objects, and I’d like to get all values (HasHit, HitTaggedObject, HitTagIndex and HitFraction) from all components.

My code is below.

public override void OnActionReceived(float[] vectorAction)

        ...

        var r1 = this.GetComponent<RayPerceptionSensorComponent3D>();
        var r2 = r1.GetRayPerceptionInput();
      
        var r3 = RayPerceptionSensor.Perceive(r2);
        {          
            foreach(RayPerceptionOutput.RayOutput rayOutput in r3.RayOutputs)
            {
                        Debug.Log(rayOutput.HasHit+" "+rayOutput.HitTaggedObject+" "+rayOutput.HitTagIndex+" "+rayOutput.HitFraction);
            }
        }

Sensor Name:

  1. RayPerceptionSensor
  2. RayPerceptionSensor1
  3. RayPerceptionSensor2

I wrote this code to get values of three Ray Perception Sensor 3D components, but I could get only values of RayPerceptionSensor.

How should I write a code to get values of RayPerceptionSensor1 and RayPerceptionSensor2? If anyone know, please let me show methods or ideas.

Thanks :slight_smile:

var rcComponents = this.GetComponents<RayPerceptionSensorComponent3D>()
will return an array of RayPerceptionSensorComponent3D. Then you can iterate over those and use your existing code.

1 Like

@celion_unity Thank you! It worked!

1 Like