How to make my puzzle turned until the rotation i want and it will stop?

Please help.

Question 1:My game is a 2D puzzle game that need to rotate the game object and until a certain angle, the box will stopped and not move again, for example game like “Plumber Game”. I tried to use box colider 2D to make the connect point between the image i put in, but I don’t know what to do in c# part.

Question 2: My puzzle should be randomize angle at the start of the game, how can i randomize it?

Please help me. Thank you.

Attach the following script to your gamobject. It’ll randomize the rotation of the object at the start of the game by a factor of 90 degrees, and then rotate the object by 90 degrees (clockwise) each time you click on it.

using UnityEngine;
 
 [RequireComponent(typeof(BoxCollider2D))]
public class Puzzle : MonoBehaviour
{
  
private void Start()
{
    Shuffle();
}
 
private void OnMouseDown()
{
    Rotate();
}
 
private void Shuffle()
{
    float randomAngle = 90 * Random.Range(1, 4);

    transform.Rotate(new Vector3(0f, 0f, randomAngle));
}
 
private void Rotate()
{
    transform.Rotate(new Vector3(0f, 0f, -90f));
}
 
}

Note that in order for OnMouseDown to work, a collider must be attached to your object.

If this answer was helpful, please click ‘Accept’.

Look at the Mathf.Clamp function. It might be what you are looking for.