Creating a jigsaw puzzle

Hi ppl,

I’ve started to use Unity recently and need some help regarding a 2D game I’m doing.

The game is a simple jigsaw puzzle. The requirements are that you have several scattered pieces, possibly in different rotations. Each piece is a cube (scaled to 1,1,0.1) and has four empty objects marking the top, bottom, left and right sides of the piece.

By using a raycast, I’m checking if I hit a piece and then I can drag the piece around the screen. So far so good. Each piece also has an array variable called Connection that keeps record of which side of the piece should connect to which side of another piece (maximum connections of 4).

I managed to change the color of both pieces as they get near each other. Now I want that the pieces are connected to each other by combining them into one single mesh. Texture is not important up till now. I sort of managed to combine the pieces, but somehow the object change size and even position. What kind of transformation I have to apply to get the two meshes near other and perfectly aligned?

Here is the code I have done so far…

function connectObject(nearestConnection : Connection){
	if(nearestConnection){
		localMesh = nearestConnection.getLocalTile().GetComponent(MeshFilter).mesh;
		foreignMesh = nearestConnection.getForeignTile().GetComponent(MeshFilter).mesh;
		
		var combinedMesh:CombineInstance[] = new CombineInstance[2];
		combinedMesh[0].mesh = localMesh;
		combinedMesh[0].transform = nearestConnection.getLocalTile().transform.localToWorldMatrix;	
		combinedMesh[1].mesh = foreignMesh;
		combinedMesh[1].transform = nearestConnection.getForeignTile().transform.localToWorldMatrix;
		
		GetComponent(MeshFilter).mesh = new Mesh();
		GetComponent(MeshFilter).mesh.CombineMeshes(combinedMesh);
		
		Destroy(nearestConnection.getForeignTile());
	}
}

Thanks for your assistance,

Regards,
Clayton

you did make get this game? I would be happy if you share it :stuck_out_tongue: