In this assignment, we will be going
through making some standard pitch zoom
style touch controls. Our controls will
be made by using a script that will
attach to the camera. Select the main
camera in the scene. Next, we will add a
car script to our camera by clicking add
component new script. We will call our
script pinch zoom. Once that is done,
click create and add. Now we can double
click on the script to open it for
editing. Let's remove the body of the
class so we have a nice clean place to
start. The principle of a pitch zoom
works as follows. We need to first make
sure we have exactly two touches on the
device. Then we need to know the
distance between those touches on both
the current frame and the previous
frame.
If the distance between the touches has
gotten smaller, the touches have moved
closer together and we should zoom out.
Similarly, if the distance between the
touches has increased, then we should
zoom
in. The first thing we need to do in
this script is to make a couple of
public float variables. These will be
used to adjust the speed at which the
zoom happens.
We need two variables so that we can
separately adjust speeds to both
perspective and orthographic
cameras. We will call these variables
perspective zoom speed and ortho zoom
speed
respectively. We will also give them a
default value
of.5. Since touch input is detected in
time with the update loop, it is usually
best to deal with that input in the
update function.
However, we only want to do anything if
there are exactly two touches on the
device. We can check this using the
input.touchcount
property. Now that we are sure that
there are exactly two touches, we can
store those touches in our own touch
variables. This way we don't have to
keep accessing the properties of the
input
class. We access the touches using the
get touch function of the input class.
Since there are only two touches
present, these will be indices zero and
one in the touches array. We can
therefore pass 0 and one to the get
touch function to get a copy of these
touches. Now that we have the touches,
we need to find out what their positions
were in the previous frame. We do this
by taking their current position and
subtracting their delta position.
We want to find the distance between the
touches in each frame and use that
knowledge to determine whether we need
to zoom in or out. To find the distance
between the touches in either frame, we
subtract one touch's position from the
other to get a vector between them. Then
from that vector, we simply find its
magnitude. We can do this in one step by
enclosing our vector subtraction in
parenthesis and using the dot operator.
We are using the term delta because it
refers to a change between two pieces of
data. Having calculated the distance
between touches in each frame, we can
now find out the differences between
those
distances. This time, the order in which
we subtract is important. We will
subtract the current frames distance
between touches from the previous frames
distance. This will result in a negative
value if the distance in the current
frame is greater than that of the
previous frame. This means that the
fingers are moving apart. If the fingers
are moving apart, then we expect to zoom
in reducing the field of view or
orthographic size of the
camera. Since we want to reduce either
the field of view or orthographic size
under these conditions, this is the
correct way to subtract the two values.
Before we can go about changing the
properties of the camera though, we need
to know if the camera is in orthographic
mode or
perspective. If it is orthographic, then
we can adjust the camera's orthographic
size by the change in the distance
multiplied by the orthographic zoom
speed. We are also going to make sure
our camera's orthographic size does not
drop below zero. If it does, the image
will invert. To prevent this, we are
going to use the max function of the
math f class. This will return whichever
is the largest of the parameters it is
given. We will give it the camera's
orthographic size and a value of
0.1. That way, if the size drops below
0.1, we can instead set it to 0.1.
Now, if the camera isn't in orthographic
mode, then we know it's in perspective
mode, and we should instead change the
field of view. We can adjust the field
of view by the same amount, but using
the perspective zoom speed
instead. We again want to limit the
field of view. However, since the field
of view represents an angle, we need to
clamp it between zero and 180. We do
this using the clamp function of the
math f class. This takes three values.
First, the value you wish to clamp. Then
the minimum value is allowed to be. And
then the maximum value it's allowed to
be. Our value is the camera's field of
view. And we want it to be between 0.1
and 179.9.
When making your own pinch zoom system,
you may want to pick more appropriate
limits for your field of view and
orthographic size. We are using the
limits shown in the script because they
are the extremes of what produces a
reasonable result. That is the end of
our script. So now it is time to test
it. We are assuming at this point that
you have a device ready and set up to
test. For instructions on how to do
this, consult the devices documentation.
We are now going to build this project
by going to file build settings. We need
to add the current scene to the build.
You may be prompted to save the scene if
you haven't
already. We will then click build and
run. As you can see, when we move our
fingers together, the camera zooms out.
And when we move them apart, we zoom in.