Author Topic: UIDragDropItem and OnTriggerEnter for object underneath  (Read 4783 times)

maaya

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
UIDragDropItem and OnTriggerEnter for object underneath
« on: February 13, 2014, 06:13:33 PM »
Hi,

I am new to Unity and NGUI but figured out quite a bit in the last few days. However I am struggling with the following scenario:

I have a Prefab with a Box Collider, RigidBody and UIDragDropItem setup, that I can move around the scene and drop on the UIDragDropContainer(also with a Box Collider).

Now, there are several of these UIDragDropContainers and I am trying to highlight the one currently beneath the object that is moving.
It looks like with the UIDragDropItem on the prefab, the OnTriggerEnter/OnCollisionEnter is not working. I understand that NGUI detects collisions with Raycasting, so do I need to enable Unity's Main Camera (currently deleted) for OnTrigger/OnCollision events to work?

Any help is much appreciated.
Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIDragDropItem and OnTriggerEnter for object underneath
« Reply #1 on: February 14, 2014, 05:26:03 AM »
You should not be using physics (OnTrigger / OnCollisionEnter) with NGUI. NGUI uses colliders for quick raycasts (as it's faster than doing math in the slow C#), but the colliders themselves are completely flat -- they have no Z. Zero scale means no collision. Raycasts work, but collision won't. At least not reliably. If you really want collisions, try giving the collider a non-zero Z size value.

maaya

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: UIDragDropItem and OnTriggerEnter for object underneath
« Reply #2 on: February 14, 2014, 01:33:59 PM »
Thank you! That is helpful. I tried changing the collider scale but it does not work with UIDragDropItem enabled. With that disabled, the OnTriggerEnter is working. Something about the UIDragDropItem seems to be interfering with OnTrigger. Any ideas?

Also could you highlight how I could do the rollover/hover effect, the NGUI-style, without Unity's collision detection?
« Last Edit: February 14, 2014, 03:48:47 PM by maaya »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIDragDropItem and OnTriggerEnter for object underneath
« Reply #3 on: February 15, 2014, 01:13:10 AM »
Well, as I said... don't use OnTrigger stuff. Do your own detection logic for what's underneath.

Also keep in mind if you're trying to do logic along the lines of "highlight what's under the dragged object", then it's even easier and all you need to do is disable the collider of the dragged object, and other things will be getting OnDragOver / OnDragOut notifications.

maaya

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: UIDragDropItem and OnTriggerEnter for object underneath
« Reply #4 on: February 17, 2014, 02:47:40 PM »
Hmm... Without a collider on the dragged object, how can you even move the object? It would not capture the mouse events.

But I got it working with the collider enabled and attaching the script with OnDragOver/Out to the objects underneath, again with colliders.

Thanks a bunch!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIDragDropItem and OnTriggerEnter for object underneath
« Reply #5 on: February 18, 2014, 07:53:06 AM »
You can use colliders, just not OnTrigger notifications. Those aren't NGUI events.

maaya

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: UIDragDropItem and OnTriggerEnter for object underneath
« Reply #6 on: February 18, 2014, 12:35:49 PM »
Gotcha. It would also be helpful to add OnDragOver/Out events to the documentation. They only show up for UIButton and UIColoredButton so I had no idea to even look them.

Thanks again.

armnotstrong

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UIDragDropItem and OnTriggerEnter for object underneath
« Reply #7 on: June 20, 2017, 04:36:55 AM »
Hi, I also meet with the same problem, have tried scale the z-aixs up of the collider and got no luck, and there is no `OnDragOver/Out` Event found, or how can I use this `OnDragOver/Out` event? Is there any sample code ?

armnotstrong

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UIDragDropItem and OnTriggerEnter for object underneath
« Reply #8 on: June 20, 2017, 04:48:03 AM »
naver mind, I just figured out that the Only thing I have to do is add the
  1.   void OnDragOver(GameObject that)
  2.     {
  3.         gameObject.transform.localScale = Vector3.one * 1.2f;
  4.         //Debug.Log(" I am being draged over by : " + that.name);
  5.     }
  6.  
  7.     void OnDragOut(GameObject that)
  8.     {
  9.         gameObject.transform.localScale = Vector3.one;
  10.     }
  11.  

this script to the object that was draged over on, and it just works  ;D