To create a 2D sprite controlled by the user with movement that is limited to within a rectangle, follow these steps:
- Create a new, empty GameObject named corner_max and position it somewhere above and to the right of player_spaceGirl. With this GameObject selected in the Hierarchy window, choose the large yellow oblong icon in the Inspector window:
Figure 9.2 – Setting a colored design time icon for a GameObject
- Duplicate the corner_max GameObject by naming the copy corner_min and position this clone somewhere below and to the left of the player-spaceGirl GameObject. The coordinates of these two GameObjects will determine the maximum and minimum bounds of movement that are permitted for the player's character.
- Modify the C# script called PlayerMove to declare some new variables at the beginning of the class:
public Transform corner_max;
public Transform corner_min;
private float x_min;
private...