playMaker

Author Topic: Detect objects at a point in 2D space?  (Read 1992 times)

jameskyle

  • Playmaker Newbie
  • *
  • Posts: 15
Detect objects at a point in 2D space?
« on: August 10, 2017, 05:57:40 AM »
Hi everyone. I apologise if this is a really simple question - it seems like it might be - but I can't figure this out.

I have an enemy character in a 2D platform game and I'm trying to have it detect if there's a hole in front of it. To do this I'm trying to find a way to check if any objects in a specific point are occupied by any gameobjects in a specific layer.

To do this I initially had a trigger zone in front of the character counting objects as they entered and left but this would act unpredictably when the character detected a hole and turned, moving the trigger zone from a space where there were zero objects inside it to a space where there was.

I'm now trying linecasting with the linecast 2D action. Unfortunately it is returning all objects it hits, regardless of the mask I apply.

Is there an obvious way to do what I'm trying to do? I feel like I'm overthinking this and there's likely a nice simple solution I just can't see.

edit: I've began using the Get Next Overlap Point 2d action which seems to be what I was looking for all along but it's detecting every overlapping object except the ground. It's all very odd.

Incidentally, the ground geometry is constructed using the AutoTileMap plugin, so it could be something that's specific to that plug-in and how it works with Unity. It's building the level geometry using tiles with sprite renderer and polygon collider 2D components. Maybe the action doesn't work with polygon collider 2D?
« Last Edit: August 10, 2017, 06:30:02 AM by jameskyle »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Detect objects at a point in 2D space?
« Reply #1 on: August 10, 2017, 07:56:52 AM »
Hi,
Have you tried to make a "invisible wall" :
Make a cube and size it a little bit bigger than the hole
Remove the Mesh Components.
Set a Tag (call it 'HoleDetector' for example)
On the Box Collider, Check 'Is Trigger'

On the enemy have a "Trigger Event"
Set Trigger to 'On Trigger Enter'
Set 'Collide Tag' to "HoleDetector"
Set the send event (call it 'HoleDetected' for example)
on next state, turn your enemy (or whatever you want it to do)

jameskyle

  • Playmaker Newbie
  • *
  • Posts: 15
Re: Detect objects at a point in 2D space?
« Reply #2 on: August 10, 2017, 11:01:58 AM »
I think I may be using get point overlap 2D wrong. I have the following settings:

Game Object: Specify game object, None
(I'm calculating the point in world space)
Position: A point in world space relative to the owner position.
Interestingly, I get a correct result (ground geometry detected) if I declare explicit values but not if I pass it a matching Vector2.
Min/Max depth: None

Layer mask: 0
Invert mask: false

Results are being logged in various variables and an event has been set for loop event and finished event (a custom event rather than just using the default FINISHED event in case this makes a difference.)

The state loops into another state that I was planning to use to check the layer of the game object being detected at the point, with a test set up for anything that would match as geometry.

I'm at a bit of a loss with this one but I'll keep plugging away at it and see if I can figure this out.

jameskyle

  • Playmaker Newbie
  • *
  • Posts: 15
Re: Detect objects at a point in 2D space?
« Reply #3 on: August 10, 2017, 11:15:08 AM »
Hi,
Have you tried to make a "invisible wall" :
Make a cube and size it a little bit bigger than the hole
Remove the Mesh Components.
Set a Tag (call it 'HoleDetector' for example)
On the Box Collider, Check 'Is Trigger'

On the enemy have a "Trigger Event"
Set Trigger to 'On Trigger Enter'
Set 'Collide Tag' to "HoleDetector"
Set the send event (call it 'HoleDetected' for example)
on next state, turn your enemy (or whatever you want it to do)
What I'm trying to build is something that I can apply to a variety of enemy characters as needed, so building colliders for each enemy specifically isn't practical and may fall apart if the player changes the environment somehow.

What I'm trying to achieve is similar to the behaviour of the red turtles from Super Mario Bros. They walk until they reach a hole then turn. I just can't detect the holes.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Detect objects at a point in 2D space?
« Reply #4 on: August 10, 2017, 01:15:52 PM »
Hi,
You only need 1 collider per hole
and you will have a trigger event on the enemy, the tag will know that there is a hole
when triggered you can do what you want, depending on your enemy

from your turtle sample, you only need a trigger event (with that tag) on your Red turtles
green turtles will continue and fall down.

if you would have a turtle on a hover board, you still use the same to hover over the hole.

If you want i can make a video to show you want i mean :)

jameskyle

  • Playmaker Newbie
  • *
  • Posts: 15
Re: Detect objects at a point in 2D space?
« Reply #5 on: August 11, 2017, 08:15:29 AM »
I was really hoping to have each enemy find any holes in front of them during gameplay rather than putting in any invisible walls but having slept on it and taken some time to re-evaluate what it is I actually need to achieve the invisible walls approach is absolutely the way I should go. Thanks for tolerating my insistence I do otherwise.

It was easy to set up as I already had a FSM I could adapt that was turning the enemy when they hit level geometry. I just added a few checks for the new colliders I added that marked where the end of the platforms are and it worked immediately.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Detect objects at a point in 2D space?
« Reply #6 on: August 11, 2017, 09:43:56 AM »
Hi,
no problem, happy to help :D