playMaker

Author Topic: How to click/touch only the top object?  (Read 2830 times)

Tricky_Widget

  • Junior Playmaker
  • **
  • Posts: 62
How to click/touch only the top object?
« 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!
« Last Edit: March 30, 2015, 12:08:37 PM by Tricky_Widget »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to click/touch only the top object?
« Reply #1 on: March 31, 2015, 01:48:06 AM »
Hi,

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

 Bye,

 Jean

Tricky_Widget

  • Junior Playmaker
  • **
  • Posts: 62
Re: How to click/touch only the top object?
« Reply #2 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!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to click/touch only the top object?
« Reply #3 on: March 31, 2015, 08:47:36 AM »
Hi,

 ok, Use the Ecosystem 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

Orjax

  • Playmaker Newbie
  • *
  • Posts: 21
  • Father/son team, self taught, love learning
Re: How to click/touch only the top object?
« Reply #4 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.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to click/touch only the top object?
« Reply #5 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