Trigger events from GUI collisions?

I’ve been trying to get the onTrigger… callbacks to work for GUI elements.

I’ve created two transforms with

  • GUITextures
  • BoxColliders with IsTrigger selected
  • RigidBodies with IsKinematic selected
  • a script that allows me to move the transforms around with the mouse.
    I added tags to my transforms (“box” and “circle”). I created OnTriggerEnter and OnTriggerExit functions that print debug messages showing what “other” is. As soon as I run the script I get a message that says one of my OnTriggerEnter functions was executed before I do anything.

I’m not able to get the onTrigger from the other transform and I can’t get the onTriggerExit to fire.

Any ideas what I could be doing wrong?

Hi, welcome to the forum!

Do any of the colliders overlap with any of the triggers at the start?

Thanks for getting back to me!

I wrote the long description below before coming to the following thought. My transforms are both at 0,0,0 and my GUITexture pixel insets are different. I’m using Texture.HitTest(vector3) to see if I’m on the GUI. Do I have to look at and move the transforms and not the textures?

If that thought was correct you can skip the rest of this:

No, they shouldn’t overlap at the start. I created the GUI objects on different sides of the screen (an iPhone screen when I get it working) then added the colliders to them. I also tried to make the colliders smaller to see if that would make a difference.

  • Square GUITexture is at pixel inset X:350, Y:60 width:50, height:50 - box collider center x,y,z: 0 - size currently x,y,z: .1
  • Circle GUITexture is at pixel inset X:60, Y:60, width:50, heigth:50 - box collider center x,y,z: 0 - size currently x,y,z: .1
    Both objects have:
  • Colliders have Material:None - Is Trigger checked.
  • Rigidbody Mass:1 - Drag:0 - Angular Drag 0.05, Use Gravity un-checked - Is Kinematic checked - Collision Detection Discrete (tried them all).

I added an OnTriggerStay callback. Now, as soon as I run the project I see my OnTriggerStay debug message. Moving the objects around doesn’t make any changes in my debug line. If I turn Is Trigger off on the circle my debug message from OnTriggerStay says other = square. If I turn Is Trigger off on the square my debug message from OnTriggerStay says other = circle.

I have two scripts -

  • Moveable that gets the mouse input in Update() and uses:
    myGUITexture.pixelInset.x = Input.mousePosition.x - (itemTexture.pixelInset.width / 2);
    myGUITexture.pixelInset.y = Input.mousePosition.y - (itemTexture.pixelInset.width / 2);
  • Triggers that just has three trigger callbacks with
    Debug.Log(“OnTriggerxxxx other =” + other.tag); return;.

Should I be able to detect collisions between gui objects?

Using the transforms got me closer. If I run my program and change the x y transform values in the inspector I can get my trigger events to fire. If I use the mouse to drag the objects to the same location the triggers don’t fire. If I change my move code so when I click someplace both objects move to the same location my triggers fire.

Any ideas why my triggers wouldn’t fire when I use the mouse to drag one of the objects over the other. I’ve changed the sizes of the box colliders to large values with no success.

I think I see your problem now - the collision detection between the GUI objects is calculated in world space, not screen space. In other words, the objects are just normal objects for the purposes of collision detection, and the position of their GUITextures onscreen doesn’t affect this at all.

I understand what you’re saying but don’t see how I’d get that to work.

What I’m seeing though is that if the positions of the two gui items are exactly the same my OnTriggerEnter and OnTriggerStay are called. If I set the values of both items to .6,.6,0 in scripts my trigger callback is executed. If I set one of the items to .61,.6,0 the callback isn’t executed. This happens even if my box colliders sizes are set to 9999,9999,9999

What do I have to do to get the onTrigger callbacks?

What I need to do is be able to load many 2d images with invisible backgrounds (.png, .jpg) and allow the user to move them around. I have to know when they hit each other but don’t want any physics interactions.

Thanks!

The OnCollisionXXX functions aren’t really suitable for use with the GUI. One possibility is to use a function like the following to detect when two GUI elements’ rectangles overlap:-

function RectsOverlap(rect1: Rect, rect2: Rect) {
	return	rect1.Contains(Vector2(rect2.x, rect2.y)) ||
		rect1.Contains(Vector2(rect2.x + rect2.width, rect2.y)) ||
		rect1.Contains(Vector2(rect2.x, rect2.y + rect2.height)) ||
		rect1.Contains(Vector2(rect2.x + rect2.width, rect2.y + rect2.height));
}

Awesome - that worked! Not sure if there was an easier way to create the rects to pass to the function. I created the rects by using the object’s transform x and y values and added the texture’s pixelInset.width / Screen.width and pixelInset.height / Screen.height.

Thanks for your help!