The camera in our game is meant to feel familiar to people who have played games in the Real Time Strategy genre before. It can be moved around by the player in three different ways:
- With the keyboard: WASD and arrow keys
- By moving the cursor to the screen edges
- By holding the middle mouse button and dragging
There is also a zoom function that lets the player zoom in and out using the mouse wheel. This works by simply moving the camera forward and backward, i.e. along its local Z-axis. (Local means that it takes into consideration the rotation of the transform. The camera is always pointing down towards the ground at 60 degrees.) The zoom is smoothed so as not to snap to the choppy rotation of the mouse wheel.
Below is the full Unity C# script:
using UnityEngine; using System.Collections; public class CameraMovement : MonoBehaviour { enum Directions { LEFT, RIGHT, FORWARD, BACKWARD, } public float moveSpeed = 10f; public float dragSpeed = 0.01f; public float zoomSpeed = 7f; public float maxZoom = 4f; public float borderMargin = 50f; public bool mouseMovementActive = true; Transform cameraTransform; Vector2 mousePrevPos = Vector2.zero; private float currentZoom = 0f; private float targetZoom = 0f; void Awake() { cameraTransform = Camera.main.transform; } void Update() { UpdateKeysMovement(); if (mouseMovementActive && !Input.GetMouseButton(2)) UpdateMouseToBorderMovement(); UpdateMousewheelMovement(); UpdateZoom(); } void MoveCamera(Directions direction) { switch (direction) { case Directions.LEFT: { MoveCameraFreely(Vector3.right * -1 * moveSpeed * Time.deltaTime); break; } case Directions.RIGHT: { MoveCameraFreely(Vector3.right * moveSpeed * Time.deltaTime); break; } case Directions.FORWARD: { MoveCameraFreely(Vector3.forward * moveSpeed * Time.deltaTime); break; } case Directions.BACKWARD: { MoveCameraFreely(Vector3.forward * -1 * moveSpeed * Time.deltaTime); break; } } } void MoveCamera(Vector2 movement) { Vector3 movement3D = new Vector3(-movement.x * dragSpeed, 0, -movement.y * dragSpeed); MoveCameraFreely(movement3D); } void MoveCameraFreely(Vector3 movement) { cameraTransform.Translate(movement, Space.World); } void Zoom(float value) { cameraTransform.Translate(Vector3.forward * value, Space.Self); } void UpdateMouseToBorderMovement() { Vector3 mousePos = Input.mousePosition; if (mousePos.x < borderMargin) MoveCamera(Directions.LEFT); if (mousePos.x > (Screen.width - borderMargin)) MoveCamera(Directions.RIGHT); if (mousePos.y > (Screen.height - borderMargin)) MoveCamera(Directions.FORWARD); if (mousePos.y < borderMargin) MoveCamera(Directions.BACKWARD); } void UpdateKeysMovement() { if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) MoveCamera(Directions.LEFT); if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) MoveCamera(Directions.RIGHT); if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) MoveCamera(Directions.FORWARD); if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) MoveCamera(Directions.BACKWARD); } void UpdateMousewheelMovement() { if (Input.GetMouseButtonDown(2)) mousePrevPos = Input.mousePosition; if (Input.GetMouseButton(2)) { Vector2 mouseDelta = (Vector2)Input.mousePosition - mousePrevPos; Vector3 movement = new Vector3(-mouseDelta.x * dragSpeed, 0, -mouseDelta.y * dragSpeed); MoveCamera(mouseDelta); mousePrevPos = Input.mousePosition; } } void UpdateZoom() { float mousewheelDelta = Input.mouseScrollDelta.y / 10f; targetZoom += mousewheelDelta * zoomSpeed; targetZoom = Mathf.Clamp(targetZoom, 0, maxZoom); if (targetZoom == currentZoom) return; float delta = targetZoom - currentZoom; delta *= 0.5f; currentZoom += delta; float zoomValue = delta; Zoom(zoomValue); } }