I have an sprite and it is draggable, the thing here, is that I want that if you push the sprite out of the visible screen, it can’t go out of it, I have tried with box colliders out of the screen, and in some part, it works, but, when you drag it to much, it pushes the sprite out of the screen, I have seen some workarouns to this, but, do you know a very efficient way to do it?.
Hi Seb
I think if you investigate the use of Mathf.Max, Mathf.Min and Mathf.Clamp you should be able to find a solution for your needs.
These would allow you to set a maximum x and y position for your sprite that would mean if you dragged it further than the bounds you set it would revert to the maximum or minimum bound you set.
//Limit the bounds within the player sprite can move
transform.position = new Vector3(Mathf.Max(-boundX + playerWidth / 2, Mathf.Min(boundX - playerWidth / 2, transform.position.x)),
Mathf.Max(-boundY + playerHeight / 2, Mathf.Min(boundY - playerHeight / 2, transform.position.y)), transform.position.z);