Develop RTS Camera

Today, I developed the RTS Camera in my game. This allows the camera to move with the mouse. When the mouse approaches the edge of the screen, the camera will move in the corresponding direction.

The code is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RTSCameraController : MonoBehaviour
{

    public float panSpeed;
    public float panBorderThickness;    //Make a buffer zone at each edge of the screen

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(Screen.height);
    }

    // Update is called once per frame
    void Update()
    {
        //Camera movement when mouse near screen edges
        Vector3 pos = transform.position;

        //Record the moving vertical variables when mouse touch the top and buttom edge
        if (Input.mousePosition.y >= Screen.height - panBorderThickness)
        {
            pos.y += panSpeed * Time.deltaTime;
        }

        if (Input.mousePosition.y <= panBorderThickness)
        {
            pos.y -= panSpeed * Time.deltaTime;
        }

        //Record the moving horizontal variables when mouse touch the left and right edge
        if (Input.mousePosition.x >= Screen.width - panBorderThickness)
        {
            pos.x += panSpeed * Time.deltaTime;
        }

        if (Input.mousePosition.x <= panBorderThickness)
        {
            pos.x -= panSpeed * Time.deltaTime;
        }

        //Tranfer the recorded position to camera
        transform.position = pos;
    }
}

Later, I need to add a setting to adjust the sensitivity of the camera.

There are many other methods that can be used to control the camera with the mouse:

1. In RTS games, players can also hold the right-click button and then move the mouse to move the camera. Usually, the camera sensitivity in this method is high, making it suitable for quickly moving the camera.

2. In Civilization, players can hold the left-click button and then move the mouse. This method does not move the camera but appears as if dragging the map.

To determine which one is better, I will need the results from gameplay tests in the future. So, for now, I will stick with the current method.

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Scroll to Top