Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Ray Perception Sensor 2D | Detect Collision With a Specific Object

Discussion in 'ML-Agents' started by Krizantemum, Mar 5, 2024.

  1. Krizantemum

    Krizantemum

    Joined:
    Oct 22, 2023
    Posts:
    2
    Hi all, I try to detect only collisions with PlayerAttacks tagged objects. I have added this to Detectable Tags as seen below, however it didn't work. how can we detect such objects with tags? Is it possible to do this from the Unity UI? Else are there any code examples? Thanks!

    upload_2024-3-5_20-11-5.png
     
  2. smallg2023

    smallg2023

    Joined:
    Sep 2, 2018
    Posts:
    154
    if you want it to ignore everything else you need to set the ray layer mask to only detect the layer the "Player Attack" tag objects are on (i.e. the tagged objects would need to be on their own layer)
     
  3. Krizantemum

    Krizantemum

    Joined:
    Oct 22, 2023
    Posts:
    2
    Hello, I have created new layer and tag for each object I want to detect.
    upload_2024-3-6_21-15-45.png

    I wanted to detect above object that I have given "Assault" tag and in "PlayerAssault" layer.

    upload_2024-3-6_21-18-36.png

    it detects every attack type including others. And I also came across an issue where if I remove "Default" from the Ray Layer Mask, it does not detect any of the attacks. What might be causing the issue?
     

    Attached Files:

  4. smallg2023

    smallg2023

    Joined:
    Sep 2, 2018
    Posts:
    154
    yes, it will still get blocked by anything in the same layer(s) that are set, the tags are for the agent to be able tell the objects apart but the ray is a normal ray cast and uses the same rules as they would if you coded one yourself, so you would need to put them all on different layers if you don't want objects in front to block objects behind etc

    also if default is showing as being required for it to actually detect anything then i suspect your actual collision shape is on a parent/child which is on the default layer rather than the object shown above