Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    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.

Can 2 agent use same grid sensor or ray perception?

Discussion in 'ML-Agents' started by Glolut, Jan 27, 2022.

  1. Glolut


    Jun 30, 2021
    Let's say I have a bot whose task is shooting enemies. It has 2 different weapons, and each weapon is controlled by a policy. The first agent collects observations (enemies' location) through a grid sensor. So can the second agent, which I made to be the parent gameObject of the first agent, also collect observations through the child grid sensor?
  2. mbaske


    Dec 31, 2017
    As far as I can tell, you'll need two distinct grid sensor components for this.

    Agents store their associated sensors in a list - so in theory it might be possible for multiple agents to reference the same sensors. However, each agent invokes the sensor's Update method, so there wouldn't really be a performance gain, because the detection logic would be run multiple times per observation step anyway. Also, I think there could be a missing reference issue with this, since GridSensorComponent creates its own list of sensors, which would be overriden during multiple CreateSensors calls coming from different agents (never tried that though).

    If you're willing to dig into ML-Agents' sensor code, you can try writing a proxy sensor. I did something like this for my audio sensor. The way this works is that there's a regular sensor running detection logic. The proxy sensor merely contains a reference to the regular sensor (which updates first and buffers its observations), forwarding the necessary method calls.
    Last edited: Jan 27, 2022
    Glolut likes this.
  3. WaxyMcRivers


    May 9, 2016
    I could see this being useful in the scenario where there are many types of weapons on some character where each weapon is autonomous - and you don't want to have multiple iterations of some sensor. I know that mbaske has much more experience with the grid sensor component, so here's how I would go about it with ray perception sensors.

    The requirements for instancing and updating the ray sensors are:
    • Calling CreateSensors() for the RayPersceptionSensorComponent at the start,
    • Using sensor.RaySensor.RayPerceptionOutput.RayOutputs to actually get the data,
    • And calling sensor.RaySensor.Update at whatever frequency you'd like (ie the decision request frequency)
    Using these you can store the data in memory and have each agent (on its own policy) access this script/data on their CollectObservations call; so you'd add the data through vector observations rather than through the traditional ray perception sensor component.

    As long as the agents are children of this script and/or you have "use child sensors" turned off just in case in your behavior parameters, I think it should work out fine. Otherwise, you could achieve the same thing by instancing your own rays and doing the data collection that way.
    Glolut likes this.