Object within bounds of another object

How do I check if an object is within bounds of another object?

I am doing a 2D game and doing some drag'n drop and have check if the object getting dragged is within the bounds of another object. If false the dragged object have to be destroyed.

There might be an easier way than using bounds and since I'm new to Unity I'm open for every kind of solution. I do prefer UnityScript though.

Thanks in advance:)

What are the shapes of the objects involved? Are they convex? Non-convex? (Etc.)

Not convex. Simply just a plane above another plane.

...and completely square. I'm coming form flash where I simply would check my rectangles or coordinates.

Squares are convex :) Anyway, if you're using the built-in physics system, you might be able to use colliders, as harmless suggested. Otherwise, you could just perform the test manually (containment tests involving rectangles and other convex polygons are pretty straightforward).

Well alright - maybe I should do some reading in geometry:) Thanks for your inputs. As you can see in my below comment I've got it working but now have some other problems.

2 Answers

2

use built-in mesh colliders like a box collider and set isKinematic to true then do an OnCollisionEnter as referenced in the docs

http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html?from=Collision

you could also roll your own system but perhaps the above is easier...

Thanks it worked but another problem occurred: When I duplicate my GameObject that hace the collider and Rigidbody attached the collision detection disappears from the cloned object.

simply post a new question:) this system wasn't intended for an evolving discussion. that is what the forums are for!

Right:) I might post another question but I think I've found a better solution

Hey - HR - I’d love to know how you did this. I come from Flash too and I don’t really get the collider thing. Did you find a way to do it with bounds? Would much rather do it that way.