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.
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.
I hope the above pseudo code helps you understand the concept.