How do you set transform boundaries ?,How do I set transform boundaries ?

I am making a game where the background is bigger than the visible play area. And I want him to be able to move to the edge of the background but no further, how do I set the boundaries so he is only able to move to the right when at the left edge…,How do I set a boundary for the player object to be able to move only to the edge of the background but not through it ?

I have a game where the background is bigger than the visible play area and the player can move further left and right, how do I set it so the player is only able to get to the edge but no further?

In my project, I made 4 clamps (+x -x +z -z) like this. (Top-view of the background)

          +z

  -x              +x

          -z

Then, I add a position-restriction to script like this.

 private void Update()
{
        SetClamps();
}

void SetClamps()
{
    float x = Mathf.Clamp(transform.position.x, X_ClmapMinus.position.x, X_ClmapPlus.position.x);
    float z = Mathf.Clamp(transform.position.z, Z_ClmapMinus.position.z, Z_ClmapPlus.position.z);

    transform.position = new Vector3(x, transform.position.y, z);
}

It is just my way to make boundaries and I hope it will help you in some way.