Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to get RayPerceptionSensor values

Discussion in 'ML-Agents' started by Twyy_Ztn, Nov 22, 2020.

  1. Twyy_Ztn

    Twyy_Ztn

    Joined:
    Aug 5, 2019
    Posts:
    9
    Hi everyone, I would like to recover the values of the "RayPerceptionSensor" how can I do?
    Moreover I know that the observations of the RayPerceptionSensor are directly added to the observations, is it possible that this is not the case?
     
  2. ShirelJosef

    ShirelJosef

    Joined:
    Nov 11, 2019
    Posts:
    21
    You can recover the values by using method: Perceive in Class RayPerceptionSensor.
    You will need the GetRayPerceptionInput() from Class RayPerceptionSensorComponentBase

    The observation are added in the file com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs
    So I see two options
    1. implement your own sensor using the ISensor interface
    2. that's kind of a long shot, but maybe put sensor in the child of the game object in which the agent script is, and tag "use child sensors" off. Then the agent wont use sensor, but you will be able to access it using Perceive
    (Never tried either, but I think it's worth a shot)
     
    samarth-robo likes this.
  3. samarth-robo

    samarth-robo

    Joined:
    Aug 13, 2020
    Posts:
    4
    Use @ShirelJosef's answer, I was able to access sensor output in Unity C# with the following code:

    Code (CSharp):
    1. var c = GetComponent<RayPerceptionSensorComponent3D>();
    2. RayPerceptionInput spec = c.GetRayPerceptionInput();
    3. RayPerceptionOutput obs = RayPerceptionSensor.Perceive(spec);
    And then use the data contained in
    RayPerceptionOutput obs
    documented here: https://docs.unity3d.com/Packages/com.unity.ml-agents@1.0/api/Unity.MLAgents.Sensors.RayPerceptionOutput.html

    H
    owever, I don't know if observations can be prevented from automatically being appended, as @Twyy_Ztn asked earlier.
     
    Last edited: Dec 7, 2020