1
\$\begingroup\$

I am trying to make a draggable GameObject that can be dropped within a container but when i'm releasing the mouse button even though the mouse is over the container (and the dragged object too) OnDrop isn't raised on my container. To make it work i must pick the dragged GameObject right at its edge so that the mouse is over the container but not the draggable when i release the mouse button

According to what I've seen drag and drop handled by unity is based on Raycasts and Raycasts could be configured to ignore objects that are in certain Layers thanks to a LayerMask the problem is that i wasn't able to find anything to apply a LayerMask

Is there a way to do this or any workaround that could let the drop target know that a dragged was dropped on him?

\$\endgroup\$
  • \$\begingroup\$ There is a checkbox called Raycast Target on certain components. So when you release the drag, your mouse is over the object that you are dragging. You can just uncheck that box on objects that you are dragging, so that raycast would go through. If you want to use LayerMask, or you want to raycast multiple targets, you can create a custom raycasting solution and your own interfaces for it. \$\endgroup\$ – Candid Moon _Max_ Jan 29 at 15:06
  • \$\begingroup\$ The first option doesn't seem to work and the second option is not realy a good idea, unity provides us something it would be like reinventing the wheel which isn't realy what you're meant to do when coding. And if the Unity's implementation doesn't allow what i want then i should just let them know and find a solution with them instead of doing everything on my own. @CandidMoon \$\endgroup\$ – nalka Jan 29 at 15:40
  • \$\begingroup\$ Could you provide more information on the problem, like screenshots, video, reference to the same kind of problem? Because 1 option should have worked with what I imagine it to be. And second option is actually about half and hour of coding, less or more depending on the scale of the system. But to get your problem to work it could take even 15 min of sole coding, if coder understands how to properly interact with UI raycasting in Unity (I don't know it yet). From experience, some things you just have to code on your own. Whatever suits you the best. \$\endgroup\$ – Candid Moon _Max_ Jan 29 at 17:30
  • \$\begingroup\$ well appart from telling you that the dragged object seems to be blocking the raycast between the mouse and the drop reciever (assuming IDropHandler is realy based on Raycasts) there's quite nothing more i can do... \$\endgroup\$ – nalka Jan 29 at 17:37
  • \$\begingroup\$ How can it block raycast if you turn off Raycast Target on its components? Do you have Canvas Group on it? Does it have Blocks Raycasts as true? \$\endgroup\$ – Candid Moon _Max_ Jan 29 at 17:45
0
\$\begingroup\$

There is a checkbox called Raycast Target on certain components or Blocks Raycasts on CanvasGroup. So when you release the drag, your mouse is over the object that you are dragging. You can just uncheck that box on objects that you are dragging, so that raycast would go through. If you want to use LayerMask, or you want to raycast multiple targets, you can create a custom raycasting solution and your own interfaces for it.

GraphicRaycaster.Raycast - this example explains pretty much how to create custom behaviour for raycasting. You could combine it with OnDrop but I would suggest just writing your own if it's required. But it's probably achievable with current system. I bet what EventSystem does is basically checks for button press, when button was pressed/released it raycasts, if object hit implements IDropHandler, then call OnDrop. With custom system you can have LayerMask which will tell what layer to hit while raycasting.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.