playMaker

Author Topic: ArrayList Find GameObjects Inside Collider  (Read 12912 times)

dudebxl

  • Hero Member
  • *****
  • Posts: 602
ArrayList Find GameObjects Inside Collider
« on: December 02, 2015, 09:43:05 AM »
Add to an array all the objects (3d or 2d) which are inside a collider. Extension to http://hutonggames.com/playmakerforum/index.php?topic=11673.0

Filter by tag (obligatory) then optionally by layer.

2 actions:
- ArrayList Find GameObjects Inside Collider
- ArrayList Find GameObjects Inside Collider2D

on ecosystem or https://snipt.net/dudebxl/
« Last Edit: December 02, 2015, 10:21:16 AM by dudebxl »

foxdeltagames

  • Junior Playmaker
  • **
  • Posts: 62
Re: ArrayList Find GameObjects Inside Collider
« Reply #1 on: December 02, 2015, 01:01:26 PM »
nice! this can come in handy :)

Acavodo

  • Playmaker Newbie
  • *
  • Posts: 7
Re: ArrayList Find GameObjects Inside Collider
« Reply #2 on: July 15, 2017, 06:52:47 AM »
Hi! Im trying to use this action for bomb effect.

Here's capture for my setting.


it is not finding any object which I tagged correctly.

I think "Collider Target" needs its own collider component right?

help !

omgitstri

  • Playmaker Newbie
  • *
  • Posts: 46
    • My Prototypes
Re: ArrayList Find GameObjects Inside Collider
« Reply #3 on: October 16, 2017, 01:34:05 PM »
Hello,

I think I found a small bug about the action, it seems like the Root GameObject requires to have a Mesh Renderer for this action to add it to the Array List.

Right now I have a gameobject (Tile) within it I have a child gameobject (Mesh) so I could easily swap out the placeholder in the future and this is causing it to not add the Tile into the Array List.

A work-around that I'm using now is to add a MeshRenderer on the root gameobject with nothing in it so the action could detect it.

Tri Nguyen
Tri Nguyen
Game Designer at Nvizzio Creations

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15594
  • Official Playmaker Support
Re: ArrayList Find GameObjects Inside Collider
« Reply #4 on: October 17, 2017, 02:29:23 AM »
Hi,

 I checked the action and yes, it's a requirement, is that a problem for your case?

 Bye,

 Jean

omgitstri

  • Playmaker Newbie
  • *
  • Posts: 46
    • My Prototypes
Re: ArrayList Find GameObjects Inside Collider
« Reply #5 on: October 17, 2017, 10:47:33 AM »
Hello,

Maybe it's just my workflow that makes me think it's a bug.
I usually have the Root contain all the scripts/FSM and have its Child be the mesh, that way i could always update the mesh whenever I want to without having to add the script/FSM again. Because of this, my Root gameobject don't have a mesh renderer and that causes the action to not work.

Tri Nguyen
Tri Nguyen
Game Designer at Nvizzio Creations

omgitstri

  • Playmaker Newbie
  • *
  • Posts: 46
    • My Prototypes
Re: ArrayList Find GameObjects Inside Collider
« Reply #6 on: October 18, 2017, 12:58:55 PM »
Hello,

The action does not seem to work if it is on a prefab that gets instantiated.
I tested this by:
  • instantiating the prefab at 0,0,0 with object tagged as 'Tile' at the same location but the Play Maker Array List Proxy does not update
  • then I tried placing the prefab at 0,0,0 beforehand without instantiating and it managed to grab the objects that are tagged as 'Tile'

Tri Nguyen
Tri Nguyen
Game Designer at Nvizzio Creations

Deek

  • Full Member
  • ***
  • Posts: 133
Re: ArrayList Find GameObjects Inside Collider
« Reply #7 on: March 11, 2018, 11:36:59 AM »
I've created two new actions based on dudebxl's derivations to be found on the Ecosystem:
"List Game Objects Inside Collider" and "List Game Objects Inside Collider 2D".
They differ in that they rely on the target GameObjects to have Collider/Collider2D components instead of Renderer/Sprite components, the 'Collider Target' variable only allows the right component to be inserted, the 'Tag'-variable only shows the existing tags as a drop-down list instead of a string input and is set to 'Untagged' by default; they optionally store the result in an FsmArray, store the amount of entries and update every frame.

I would also propose, that the actions by dudebxl actually say that the target GameObjects need to have a Renderer component and for the 2D version a Sprite component somewhere, because they are otherwise confusing and don't seem to work if you don't know that.

...

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
Re: ArrayList Find GameObjects Inside Collider
« Reply #8 on: April 11, 2018, 03:28:24 PM »
I've created two new actions based on dudebxl's derivations to be found on the Ecosystem:
"List Game Objects Inside Collider" and "List Game Objects Inside Collider 2D".
They differ in that they rely on the target GameObjects to have Collider/Collider2D components instead of Renderer/Sprite components, the 'Collider Target' variable only allows the right component to be inserted, the 'Tag'-variable only shows the existing tags as a drop-down list instead of a string input and is set to 'Untagged' by default; they optionally store the result in an FsmArray, store the amount of entries and update every frame.

I would also propose, that the actions by dudebxl actually say that the target GameObjects need to have a Renderer component and for the 2D version a Sprite component somewhere, because they are otherwise confusing and don't seem to work if you don't know that.

Am i doing this right?



I don't seem to be getting anything :(

DanielThomas

  • Beta Group
  • Full Member
  • *
  • Posts: 150
Re: ArrayList Find GameObjects Inside Collider
« Reply #9 on: April 13, 2018, 01:05:10 PM »
Just wanted to say I get the same problem as krmko, nothing shows up. Anyone who can confirm they got it to work?

Deek

  • Full Member
  • ***
  • Posts: 133
Re: ArrayList Find GameObjects Inside Collider
« Reply #10 on: April 13, 2018, 06:25:30 PM »
Yup, that was a mistake on my part.
In the original actions the GameObjects in the scene got searched by tag with the function "GameObject.FindGameObjectsWithTag()", which requires a tag to be specified and doesn't seem to allow the 'Untagged'-tag.
Since I naively assumed that it would just work to set the 'Untagged'-tag by default and only tested both actions with a specific tag, I didn't think 'Untagged' would cause any trouble.

I fixed this problem, which means you'd need to re-download the actions from the Ecosystem. Sorry for the inconvenience.

I also re-worked both actions to not only allow to filter by 'Untagged'-GameObjects but also made that tag filter completely optional, meaning you now can set the tag to 'None' and it ignores that tag filter, resulting in listing every GameObject regardless of the tag. The layer-filter is unaffected and can be used in conjunction with the tag-layer to narrow down, which GameObjects to capture.
Additionally, I implemented an error check when the collider target wasn't set and that when toggling the tag from 'None' to the tag-list it shows 'Untagged' instead of an empty entry (uses OnGUI so it might not always do that, but that would be just a minor inconvenience if not).


P.S.: If anyone is interested to know how any of these actions work (for clarity or performance reasons), here a short rundown:
- the action searches for each active GameObject in the scene (dudebxl's versions directly by tag, which is slightly more performant but forces you to define a tag, while mine search for any Object of type GameObject; in either case these actions should be repeated/used as scarcely as possible to reduce that overhead of constantly going through all GameObjects in the scene);
- it checks for each found GameObject if it matches the specified tag and layer;
- skips any GameObject that doesn't contain an appropriate collider component;
- gets the bounds of each collider, sees if it intersects the bounds of the user-defined collider and only lists the GameObjects that match all these criteria

agelvik

  • Beta Group
  • Playmaker Newbie
  • *
  • Posts: 46
Re: ArrayList Find GameObjects Inside Collider
« Reply #11 on: February 18, 2019, 04:50:35 PM »
I've created two new actions based on dudebxl's derivations to be found on the Ecosystem:
"List Game Objects Inside Collider" and "List Game Objects Inside Collider 2D".
They differ in that they rely on the target GameObjects to have Collider/Collider2D components instead of Renderer/Sprite components, the 'Collider Target' variable only allows the right component to be inserted, the 'Tag'-variable only shows the existing tags as a drop-down list instead of a string input and is set to 'Untagged' by default; they optionally store the result in an FsmArray, store the amount of entries and update every frame.

I would also propose, that the actions by dudebxl actually say that the target GameObjects need to have a Renderer component and for the 2D version a Sprite component somewhere, because they are otherwise confusing and don't seem to work if you don't know that.

Hey,
The script works well except when the collider gets rotated.

Seems like the action doesn't list objects accurately when it's rotated which results in undesired results... Instead of listing objects according to the colliders shape, it translates to this square shape and list objects that are expected to be outside of it.
I'm using "List Game Objects Inside Collider" btw, don't know if same applies to the 2D version

Would be awesome if this could get fixed!
« Last Edit: February 18, 2019, 04:59:36 PM by agelvik »

Deek

  • Full Member
  • ***
  • Posts: 133
Re: ArrayList Find GameObjects Inside Collider
« Reply #12 on: February 20, 2019, 03:26:03 PM »
@agelvik

Yea..., apparently the Bounds.Intersects() method I used in both actions doesn't play nicely with rotated objects since it uses the root rotation instead of the local one. You can actually find a few posts on the Unity forum mentioning this behavior, but it seems to be like that by design.
I've found a few possible solutions involving Raycasting to detect overlapping areas, so I will try to implement them on the weekend.
If one of them work, I either replace the current behavior or add a checkbox to use the Raycast version for detecting rotated objects accurately, as it is usually less efficient. I also remember creating a script that checks if NGUI sprites are in view by using the camera's frustum, so that might also be an option for this.

I didn't know of this behavior so thanks for pointing it out and I'll see if I can find a fix or better solution for that.
« Last Edit: February 20, 2019, 03:28:28 PM by Deek »

agelvik

  • Beta Group
  • Playmaker Newbie
  • *
  • Posts: 46
Re: ArrayList Find GameObjects Inside Collider
« Reply #13 on: February 21, 2019, 10:43:39 AM »
Much appreciated, thanks!!

Deek

  • Full Member
  • ***
  • Posts: 133
Re: ArrayList Find GameObjects Inside Collider
« Reply #14 on: February 23, 2019, 07:07:33 PM »
I now improved both versions to more accurately detect colliding GameObjects even with a rotated source GameObject. The 2D version is way more accurate in regard to box colliders, since it uses a built-in function for detecting overlapping colliders and on the 3D version I had to manually adjust the extents of the collider, which means that there can still be a small gap between two colliders in which the second one gets recognized (and judging by your screenshot, you probably should use 2D colliders anyway  ::)).
Each should be a bit more performant, because they don't go through each GameObject in the scene anymore. On top of that, I also added a filter to ignore triggers and removed the "Incl Layer Filter" checkbox by making the Layer field start as 'None', as well as other minor improvements.

They have been updated in the Ecosystem and are also available in the attachments.