Snap inside drop area?

I have here “cucumber” game object and placing it on rack. Now some parts of object may get visible outside of cupboard. Now I want to achieve something like snap, so when object is placed on rack and if some part is outside then snap it into inside and place it.

Also, a way to find whether some object will fit inside this space or not.

Any suggestions on this, can’t think solutions for this how to achieve it.

Snapobject

What if you for each object (cucumber, tomato etc) calculate a bounding box, and then check with a bounding box for the “racks”. Then you can check if they’re outside of the rack or the cupboard.

2 Likes

Got it. I will try to implement this way. Thanks.

Vikash,

In this post you mention successfully limiting an object’s draggable range. Presumably you do this by checking if an object’s X position is greater than one certain value and smaller than another value.

This code can easily be adapted. Instead of just checking the X position of the object, you need to check the X position and the width of the sprite (we will divide the width of the sprite by two).

assume the cupboard goes from 200 to 400.
x.pos is the X position of the cucumber.
x.width is the width of the cucumber.

if x.pos - (x.width/2) > 200 then
– the left side of the object is inside the boundary
if x.pos +(x.width/2) < 400 then
– the right side of the object is within the boundary.
end
end

I hope the above pseudo code helps you understand the concept.

3 Likes