Unity ML Agents Ray Perception Sensor 2D not showing collisions


I am still relatively new to the Unity environment and am currently working with reinforcement learning and ML agents. For this I wanted to add an agent to the 2D platformer.

I have attached two ray perception sensors to my agent. Unfortunately I can’t get any hits with these sensors, at least they are not displayed as usual with a sphere in the gizmos.

Scene view with gizmos

The sensors are casting rays, but like you see in the image, they are not colliding.

The ray perception sensor are childs of the agent, defined in its prefab.
I defined the sensors to collide with 4 tags: Untagged, ground, enemy and coin

I assigned the coin tag to the token, the enemy tag to the enemy and the ground tag to the tilemap forming the ground.
The token has a circle collider, while the enemy has an capsule collider.
On the tilemap there is a tilmap collider.

I would now expect the sensor to collide with the token, enemy and ground and display these hits in spheres, but it does not.
So, what am I doing wrong?

Definition of RayPerceptionSensor2D


After a lot more investigation i figured out the problem myself:

The tags where correctly configured, but i had an misunderstanding in the Ray Layer Mask.

Ray Layer Mask

Previously i had configured it to "Everything"/"Default" which resulted in a collision in the sensor itself and seems not right (Despite the player tag was not in the detagtable tags).

After i created more layers and assigned my targets to these layers, everything starts working as intended.

Working Result

Maybe this answer will help someone, having similar issues.

Leave a Reply

Your email address will not be published. Required fields are marked *