I've been sort of trying my own thing but it's super buggy so I'd like to see what other people have done to create moveable spaces. In essence, I just want to restrict my character's movement to inside of a sprite. So one of my sprites is the character and another one of my sprites is a box -- I want to make it so my character can't leave the box. I've been modifying transform.position on mouse click in order to move my character, but now I'm not sure if this was the smartest idea.
2 ideas came to my mind. Look at illustrations:
First option is obvious, add colliders to boundaries of your game area.
The second option is, to limit your characters movement inside boundaries within your script.
For example, if you have a code that controls the position of character, check if your values falls inside the boundaries of your game area, then clamp it to max and min values if it exceeds.
Thankfully, unity has this feature built-in in Mathf library with "Mathf.Clamp" function. So in this example you need to check like this:
var finalVertical = Mathf.Clamp (rawVertical, -c, +c); var finalHorizontal = Mathf.Clamp (rawHorizontal, -a, +b);
For more information about clamp, check this out: