Skip to main content

Camera

During a match, a player is not more than a placeholder object, controlling its own, local camera using the CameraController script. In fact, a player does not really exist, since the original tower defense genre does not include player characters or avatars that could see each other. For example, if you would like to include a hero feature with players that can walk around in the scene, you would want to instantiate a visual representation of a player along with a separate Player script attached to it.

As mentioned in the previous Lobby section, the player data is persisted from the Lobby to the map scene, but there is no player object spawned for the client they can control. Without the need for a separate Player script, this makes the CameraController script the main component for the player's navigation across the map.

The CameraController component is attached to the Main Camera object and provides very basic navigation with the following input:

KeyDescription
WNavigate forward.
SNavigate backward.
ANavigate left.
DNavigate right.

You would assume that the camera is moved using that input - but actually, it is not! The input is applied to a Vector3 position variable, which then the camera follows. This is because of the additional distance and height logic the camera uses. If we would modify the camera position directly, we would need to take that into account as well, making the script more complicated. Now, we can simply set the target position where the camera should look at and the script does the rest.

Moving the target position happens using a Vector2 variable, where (1, 0) would be right, (-1, 0) is left, and the same with up/down on the y-axis. Building it that way allows us to have one logic that works cross-platform: supporting keyboard and touch input. Speaking of touch input, on mobile devices there is an additional scrolling area on each side of the play-screen with a UIScrollArea component attached to it. This reacts to the player pressing the UI element area and modifies the Vector2 variable according to the desired direction.

PlayerCamera010