- Home /
Creating jigsaw puzzle in Unity
Hi! i am new to unity and i want to create jigsaw puzzle in unity and i have no clue how to make it work. for example, i want to drag and drop my puzzle piece onto board(plane board) and when it is in right position or close enough to that position, that piece should fit and stay in that position and if all the pieces are in right position, the game should win. I already have the dragging script for piece but i don't know how to make it stay in the right position. here's the dragging script
public class DragObject1 : MonoBehaviour { void OnMouseDrag() { float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z; Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen)); transform.position = new Vector3(pos_move.x, pos_move.y, pos_move.z); } }
Answer by EccWall · Aug 12, 2019 at 05:07 PM
@Zerox795 You can use a empty game objects to organize locations for each piece object and help of box collider and rigidbody 2D you can code if a piece objects collides with its empty object that you created for location, piece will stay there. something like;
void OnTriggerEnter2D(Collider2D other) { if(other.gameObject.name == gameObject.name ) { transform.position == other.gameObject.transform.location; } }
This code is not mine, I found it.
Your answer
Follow this Question
Related Questions
Check to see if item is in correct slot 1 Answer
Jigsaw Puzzle Drag & Drop issues. 0 Answers
Dragging one object, but when it overlaps another it sticks also 0 Answers
Drag and Drop "Puzzle" Game con't always work 0 Answers
match 3 puzzle game 1 Answer