Game Development Reference
In-Depth Information
2.3 Reading mouse input
After we have learned how to read and use keyboard input, let's now move to the other major
input device in PC games: the mouse. If you are interested in computer games, you defin-
itely know the importance of this device for these games. For instance, it is a major input
device in shooting games, and it is also used to give tons of commands in real-time strategy
games. Additionally, it is the major input device when it comes to game menus and the user
interface in general.
What is interesting for us in this section is reading two-dimensional movement of the mouse, in
addition to various mouse buttons and mouse wheel scrolling. Let's begin with a new simple
scene that has one object: a sphere located at the origin. We will add a script to this sphere
that reads mouse movement and interprets it to displacement of the sphere on x and y axes.
The script will also read the clicks on mouse buttons and use them to change the color of
the sphere. Finally, mouse wheel scrolling will be used to change the scale of the sphere.
Before we begin, it is advised that you position the camera in a relatively far distance from
the sphere (0, 0, -70 for example), which should prevent the sphere from leaving the field of
view easily. Listing 8 shows the code required to read mouse input and interpret it to achieve
the desired behavior.
Search WWH ::




Custom Search