Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question How to check Ray perception Sensor 3D?

Discussion in 'ML-Agents' started by Kittisakrr, Apr 2, 2023.

  1. Kittisakrr

    Kittisakrr

    Joined:
    Feb 9, 2021
    Posts:
    8
    I want to know what value is sent by each Ray Perception. What should I do?
     
  2. Hallahallan

    Hallahallan

    Joined:
    Jan 16, 2023
    Posts:
    7
    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<RayPerceptionSensor>();

    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
     
  3. kokimitsunami

    kokimitsunami

    Joined:
    Sep 2, 2021
    Posts:
    25
    You can check the values from Ray Perception Sensor like below:

    Code (CSharp):
    1. using Unity.MLAgents.Sensors;
    2.  
    3. private void checkRayCast()
    4. {
    5.         RayPerceptionSensorComponent3D m_rayPerceptionSensorComponent3D = GetComponent<RayPerceptionSensorComponent3D>();
    6.  
    7.         var rayOutputs = RayPerceptionSensor.Perceive(m_rayPerceptionSensorComponent3D.GetRayPerceptionInput()).RayOutputs;
    8.         int lengthOfRayOutputs = rayOutputs.Length;
    9.  
    10.         // Alternating Ray Order: it gives an order of
    11.         // (0, -delta, delta, -2delta, 2delta, ..., -ndelta, ndelta)
    12.         // index 0 indicates the center of raycasts
    13.         for (int i = 0; i < lengthOfRayOutputs; i++)
    14.         {
    15.             GameObject goHit = rayOutputs[i].HitGameObject;
    16.             if (goHit != null)
    17.             {
    18.                 var rayDirection = rayOutputs[i].EndPositionWorld - rayOutputs[i].StartPositionWorld;
    19.                 var scaledRayLength = rayDirection.magnitude;
    20.                 float rayHitDistance = rayOutputs[i].HitFraction * scaledRayLength;
    21.  
    22.                 // Print info:
    23.                 string dispStr = "";
    24.                 dispStr = dispStr + "__RayPerceptionSensor - HitInfo__:\r\n";
    25.                 dispStr = dispStr + "GameObject name: " + goHit.name + "\r\n";
    26.                 dispStr = dispStr + "Hit distance of Ray: " + rayHitDistance + "\r\n";
    27.                 dispStr = dispStr + "GameObject tag: " + goHit.tag + "\r\n";
    28.                 Debug.Log(dispStr);
    29.             }
    30.         }
    31. }
    32.  
    Hope this helps.
     
    Last edited: May 22, 2023
    kaizoku_04 and pradajohn like this.
  4. kaizoku_04

    kaizoku_04

    Joined:
    Feb 19, 2024
    Posts:
    1
    thank you so much it worked