Author Topic: Detect widget collision  (Read 6626 times)

vikti

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 25
    • View Profile
Detect widget collision
« on: February 27, 2015, 05:00:14 PM »
Hello,

I have a map with UIWidget markers. I need to detect collision between them (eg : MarkerPlayer entering GruntPlayer) and detect click, drag, etc...

So each of my marker have a rigidbody and a collider. But it does not fire anymore any OnClick or other "regular" events.

Others infos :
-each collider has 0 on z size.
-I use a modified version of UITriggerEvent but it works well when I remove the rigidbody
-Does not work with UICamera set to 3D UI neither 3D world.
-All my markers are anchored to external gameobjects.
-collider's IsTrigger = true;

Where I am wrong ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detect widget collision
« Reply #1 on: March 01, 2015, 04:46:53 PM »
UI elements aren't set up for collisions with each other by default. To make them collide, you need to use 2D colliders instead of 3D, and you need to make them non-triggers. NGUI uses trigger colliders by default.

vikti

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Detect widget collision
« Reply #2 on: March 02, 2015, 04:27:24 AM »
Ok I clean this. Now all my UIWidget markers have a non trigger 2d collider.
The OnClick came back, but I have no more collision.

I change OnCollisionEnter to OnCollisionEnter2D in my UITriggerEvent-like component :

  1. void OnCollisionEnter2D (Collision2D collision)
  2. {
  3.         Debug.Log("collide");
  4.  
  5.         if (Current != null || BlockSending) return;
  6.  
  7.         Current = this;
  8.         CollisionEnterSignal.Dispatch(collision);
  9.         Current = null;
  10. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detect widget collision
« Reply #3 on: March 03, 2015, 05:25:02 PM »
Make sure the widgets don't belong to the same rigidbody. NGUI will add one on the Panel by default. Colliders belonging to the same rigidbody can't collide.

vikti

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Detect widget collision
« Reply #4 on: March 04, 2015, 12:35:05 PM »
I cleaned this too. No parent with a rigidbody. But still not any collides.

Here is a screen :

The blue marker with a dagger is the player. I have attached a little script (Move) to drag it and simulate the player movement.

When the Widget collides a yellow one it should trigger the OnCollistionEnter2D function.

All my markers are the same except they don't have the Move script attached.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detect widget collision
« Reply #5 on: March 05, 2015, 09:58:24 PM »
Did you check the physics layers to make sure that the layer can actually collide with itself?

vikti

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Detect widget collision
« Reply #6 on: March 06, 2015, 03:52:19 AM »
Yes it is checked.

I manage to get the collision back by adding a rigidbody on my marker player. But now it spin around like a 2d physical object...

Is there any exemple for my case in your samples ? I am really stuck :/

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detect widget collision
« Reply #7 on: March 10, 2015, 07:38:00 PM »
None of the examples would be helpful in what you're attempting to do. Structure your UI like so:

UIRoot (make sure there is no Rigidbody here)
- Object 1 (rigidbody, UIPanel)
-- Widget or Sprite (Collider)
- Object 2 (rigidbody, UIPanel)
-- Widget or Sprite (Collider)

vikti

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Detect widget collision
« Reply #8 on: March 15, 2015, 10:42:08 AM »
If I use a panel on each marker my draw calls number will explode because I could have a lot of markers on my map. (a panel = 1draw call, right ?)

I am at this point :

- _mapMarkersRoot
-- _playerMarker (UIWidget, rigidbody, 2D collider)
-- _itemMarker (UIWidget, 2D collider)
-- _itemMarker (UIWidget, 2D collider)
...

Now I get both collide and click detection, but very very strange behaviors happens with the marker players :
  • _markerPlayer hits the buttons (the squares at screen bottom) when I drag the marker vertically on them, but without hitting them.
  • the click event may be in conflict with another event. I have to click/drag several times on the _playerMaker to get the click event. This does not happen when I remove the rigidbody.

« Last Edit: March 16, 2015, 03:28:51 AM by vikti »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detect widget collision
« Reply #9 on: March 16, 2015, 09:19:30 PM »
UIRoot has a very tiny scale. If you want to use collision with your UI, you probably shouldn't use UIRoot at all. Unity's 2D physics system doesn't work well with tiny scales.

vikti

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Detect widget collision
« Reply #10 on: March 17, 2015, 03:33:54 AM »
Interesting. I think I will look for another way to design this.
Actually I tried something different and it works for now. My collisions are handled by the map objects invisible markers (on which NGUI anchors to) and my click/drag/etc by NGUI.

But I am curious about your idea to not use UIRoot at all. Is is a best practice with NGUI ??

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detect widget collision
« Reply #11 on: March 19, 2015, 06:25:50 AM »
NGUI is a UI system, so ideally you would use the UIRoot. However you aren't trying to use it as a UI system. You're trying to use it as a sprite system instead, which is why UIRoot wouldn't make that much sense.