Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: Tricky_Widget on March 30, 2015, 12:05:35 PM

Title: How to click/touch only the top object?
Post by: Tricky_Widget on March 30, 2015, 12:05:35 PM
I have a 2D game with overlapping objects.  By default, using Mouse Pick 2d Event and Touch Object 2d Event registers for all objects at the click/touch location.  But I need to be able to click/touch and only have it register on the top object and not on the lower objects (top being the lowest Z position/closest to the camera).

For example, if I have objectA at Z -1 and objectB at Z -3, I only want to select objectB (and not objectA).

Does anyone know how to do this?  Thanks!
Title: Re: How to click/touch only the top object?
Post by: jeanfabre on March 31, 2015, 01:48:06 AM
Hi,

 Then you should use raycasting, this will return the first top most object.

 Bye,

 Jean
Title: Re: How to click/touch only the top object?
Post by: Tricky_Widget on March 31, 2015, 07:55:47 AM
Then you should use raycasting, this will return the first top most object.

Thanks for the tip!

That makes sense, but how would I capture the mouse click to begin the raycast?

I'm developing for mobile, so CPU use is a major consideration.  If every object that got the click did its own raycast, that could be expensive.  But if I did one big overlay object to handle all clicks, then it could be expensive to sort through the top objects that aren't clickable to get to the one that is.

Thanks again!
Title: Re: How to click/touch only the top object?
Post by: jeanfabre on March 31, 2015, 08:47:36 AM
Hi,

 ok, Use the Ecosystem (https://hutonggames.fogbugz.com/default.asp?W1181) to get them actions.

-- Get the mouse position on screen (action "GetMousePosition")
-- ray cast using that position ( action "RayCastFromScreen")

that's it.

If perfs is an issue, raycasting should ONLY be done when you need it and from one place, there is no need for every object to raycast, only raycast from one fsm, maybe on the camera, which makes the most sense.

 Bye,

 Jean
Title: Re: How to click/touch only the top object?
Post by: Orjax on April 26, 2017, 02:11:34 PM
not sure about 2d, but i found a solution that works in my3d game might help someone.

Create a 3d box, put it just behind the button in world space. size it. turn mesh render off, then make a child of canvas.  It allows button push, but the box collider blocks objects behind it.
Title: Re: How to click/touch only the top object?
Post by: jeanfabre on April 27, 2017, 03:26:16 AM
Hi,

 the same goes with 2d, you just create an invisible collider of the appropriate size and disable interaction on your actual sprite of object.

Bye,

 Jean