Basic character movement in 2D (within a bounding rectangle) is a core skill for many 2D games, so this first recipe will illustrate how to achieve these features for a 2D game. The remaining recipes will then build on this approach for 3D games.
Since in Chapter 3, Inventory and Advanced UIs, we created a basic 2D game template, we'll adapt this game to restrict the movement to a bounding rectangle:
Figure 9.1 – Movement of the character within the rectangular area