Guys/girls, I’m making a 2D puzzle game for kids which they can drag small pieces (there are many kind of shapes like square, rectangle, triangle, circle, L-shape, half moon…) and drop them to a big shape to fill it up (the shape may be a polygonal).
The question is how can I make the orange piece become auto fit to every edges of square when the shape is near the edges, like the situation when we hover a metal piece close to a magnet and it’s sticked.
Further more, I use OnMouseDrag() function to drag and drop objects, it works good on PC with mouse but when I build on mobile device, may be it also works on touch screen ?
(This is using words from new UI but is not related)
Your canvas (the grey rectangle) should have anchor points, simply some empty game objects (Transform). When the button is released you would check if the orange piece is close enough to any of those anchor points and if so you start a lerp process to the point, or you snap there.
private Transform[] anchor;
private Transform target = null;
void Start()
{
anchor = Manager.GetAnchor(); // This returns the array of anchor you defined somewhere else
}
void Update()
{
if(lerp)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, ratio);
if(transform.position == target.position){lerp = false;}
}
// More code
}
void OnMouseUp()
{
target = GetAnchor();
if(target != null)
{
lerp = true;
}
}
private Transform GetAnchor()
{
Transform closest = null;
float distance = Mathf.Infinity;
float range = 0.2f;
foreach(Transform t in anchor) // iterate all anchors
{
float dist = Vector3.Distance(transform.position, t.position);
if(dist < range && dist < distance) // check if close enough and closer than any other already checked points.
{
closest = t;
distance = dist;
}
}
return closest;
}
Thank you! Great idea! But this is the first time I heard "anchor point", what is this and how to use it. Is it a GUI element or a game object ?
It is a concept that is used anywhere that you need to snap something to someplace. It is used in the new UI but fafa isn't talking about that. He just means an object at the place you want your puzzle piece to snap to. To be able to check if it's at the correct place.
thanks you guys :) but I'm using Unity 4.5.4, so I think I don't have the New UI. Is there anyway to do without download the 4.6 beta version ? And further more, all my pieces and shapes are 2D sprite cut off from a big image
Thank you! Great idea! But this is the first time I heard "anchor point", what is this and how to use it. Is it a GUI element or a game object ?
– BraveVNIt is a concept that is used anywhere that you need to snap something to someplace. It is used in the new UI but fafa isn't talking about that. He just means an object at the place you want your puzzle piece to snap to. To be able to check if it's at the correct place.
– screenname_takenthanks you guys :) but I'm using Unity 4.5.4, so I think I don't have the New UI. Is there anyway to do without download the 4.6 beta version ? And further more, all my pieces and shapes are 2D sprite cut off from a big image
– BraveVNYes, I got it! Thank you, your instructions are very detail. You're a nice coder :D Thanks
– BraveVN