Search Unity

Question How can I get the Vector3 position of RayPerceptionSensor3D ray-hit?

Discussion in 'ML-Agents' started by StewedHarry, Aug 11, 2020.

  1. StewedHarry

    StewedHarry

    Joined:
    Jan 20, 2020
    Posts:
    45
    I am trying to convert the location of a ray from a RayPerceptionSensor component, however i'm having difficulties.

    Here is the code:

    Code (CSharp):
    1. public class TestScript
    2.     {
    3.         public void GetSensorStuff()
    4.         {
    5.             // RayPerceptionSensorComponent attached to child Gameobject of
    6.             // agent Gameobject.
    7.             // This object is rotated to move sensor around
    8.             RayPerceptionSensorComponent3D rayComponent
    9.                  = childObject.GetComponent<RayPerceptionSensorComponent3D >;
    10.        
    11.             var lengthOfRayOutputs = RayPerceptionSensor
    12.                 .Perceive(rayComponent.GetRayPerceptionInput())
    13.                 .RayOutputs
    14.                 .Length;
    15.          
    16.                
    17.             List<float[]> rayBuffers = new List<float[]>()
    18.             {
    19.                 new float[(2 + 2) * lengthOfRayOutputs],
    20.                 new float[(2 + 2) * lengthOfRayOutputs],
    21.                 new float[(2 + 2) * lengthOfRayOutputs],
    22.                 new float[(2 + 2) * lengthOfRayOutputs],
    23.                 new float[(2 + 2) * lengthOfRayOutputs]
    24.             };
    25.  
    26.             var rayOutputs = RayPerceptionSensor
    27.                 .Perceive(rayComponent.GetRayPerceptionInput())
    28.                 .RayOutputs;
    29.            
    30.             // Adds subset array to each buffer for each rayoutput
    31.             for (int i = 0; i < 5; i++)
    32.                 rayOutputs[i].ToFloatArray(2, 0, rayBuffers[i]);
    33.            
    34.             // Get just the distances to a new float array which
    35.             // represents all the ray cast distances
    36.             var distances1 = rayBuffers[0][3];
    37.             var distances2 = rayBuffers[1][3];
    38.             var distances3 = rayBuffers[2][3];
    39.             var distances4 = rayBuffers[3][3];
    40.             var distances5 = rayBuffers[4][3];
    41.            
    42.             // I want to convert these distances to the Vector3
    43.             // locations where the rays hit.
    44.             // Assuming this script is attached to gameobject the rays are
    45.             // cast from:
    46.             RayCastHitLocation(
    47.                 childObject.transform.rotation,
    48.                 childObject.transform.position,
    49.                 distances1.ReverseNormalise(rayComponent.RayLength)
    50.             );
    51.            
    52.         }
    53.        
    54.         public static Vector3 RayCastHitLocation(Quaternion rotation, Vector3
    55.         position, float distance)
    56.         {
    57.             Vector3 direction = rotation * Vector3.forward;
    58.             return position - (direction  * distance);
    59.         }
    60.        
    61.     }
    This code successfully allows me to get the distances of each ray to their hit point.

    The RayCastHitLocation is supposed to give me the location of the ray hit, however it is producing incorrect results.

    It seems to give me the correct reading along the X axis. As I pan the agent left and right the Vector3.x number stays the same at the correct position. However as I move the agent along the z axis, the z position of the Vector3 increases or decreases (it should stay the same as the position of the hit point does not move).

    Is there anything in my code which could be producing the wrong result. Or, is there a better way of getting the locations of the ray hits?