Game Development Reference
In-Depth Information
After that we go into the update loop and start to read inputs sequentially. We begin with
lines 28 through 37, in which we check whether the player is pressing one of the three
mouse buttons. Input. GetMouseButton() function does the job for us, and all we have to
do is to call it and pass to it the number of the mouse button we want to check. In the de-
fault mouse setup these numbers are 0 for the left button, 1 for the right button, and 2 for
the middle button. What we do in these lines is simply changing the material color of the
sphere based on which button is pressed.
In lines 40 through 43 we compute displacement distance of the cursor by subtracting its
previous position from its current position. We then move the object on x and y axes by
the computed displacement multiplied by movement speed. Since the mouse pointer position
is by nature two dimensional, we do not include the z member of mouse position in our
computations. After that, in line 46, we update the last position of the mouse and make it
equal to the mouse position in the current frame. By doing this, we become ready to compute
mouse displacement in the coming frame.
The last step is to read the mouse scroll wheel, which is performed through Input.GetAxis() .
We pass to this function the name of the axis we want to read. Axes names can be set up
in a custom window that we might discuss later. However, we have already an axis named
Mouse ScrollWheel defined for us by Unity, so all we have to do is to pass that name to
Input.GetAxis() . If there is no wheel input, the returned value is zero. On the other hand,
scrolling up will return 1 and scrolling down will return -1. Based on the return value, we
add or subtract a vector of scale 1 from transform.localScale vector of the object. The com-
plete scene is available in scene4 in the accompanying project.
Search WWH ::




Custom Search