Working RTS camera script

Hello.

For the last month, I’ve been trying to create good RTS “Grab 'n drag” styled camera. After realising how painfully hard it is, I decided to post my script here, so people can use it and not worry about writing their own.

Here’s the script and a little instruction below:

(The zooming isn’t included yet)

using UnityEngine;
using System.Collections;

public class GalCamv2 : MonoBehaviour {
	
	//Variables----------------------------------
	
	public Transform Cam;
	public Transform RotationPoint;
	public Transform Parent;
	
	public float ySpeed = 400f;
	public float xSpeed = 400f;
	
	private float x;
	private float y;
	
	private float x2;
	private float y2;
	
	//------------------------------------------
	
	
	void Start () {
	
		Vector3 CamPos = new Vector3 (0, MinDis, 0);
		Cam.position = CamPos;
		
	}
	
	void Update () {
		
		//Parents and stuff------------------------
		
		if(!Input.GetMouseButtonUp(0)  !Input.GetMouseButton(1)){
		
			Vector3 campos = Parent.position;
			
		    Cam.position = campos;
			
		}	
		
		if(Input.GetMouseButtonUp(0)){
		
			Vector3 ParPos = Cam.position;
			
		    Parent.position = ParPos;
			
		}	
		
		if(Input.GetMouseButtonDown(0) | Input.GetMouseButtonUp(1)){
		
			x = 0;
			y = 0;
			
		}	
			
		//--------------------------------------
		
		//Movement------------------------
		
		if(Input.GetMouseButton(0)  PauseChange == 0f){
			
			Cam.parent = Parent;
			
		   x+=Input.GetAxis("Mouse X") * xSpeed * 0.02f ;
		   y-=Input.GetAxis("Mouse Y") * ySpeed * 0.02f ;
		   
			Vector3 CamLocPos = new Vector3 (-x , 0, y);
		   
		   Cam.localPosition = CamLocPos;
		}
		
		//--------------------------------------
		
		//RotationPoint-----------------
		
		if(!Input.GetMouseButton(0)  !Input.GetMouseButton(1) ){
			
			Cam.parent = null;
			
			RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit, 1000)){
            RotationPoint.position = hit.point;
            }
		}
		
		
		//--------------------------------------
		
		//Rotation-------------------------------
		
		if(Input.GetMouseButton(1)  PauseChange == 0f){
			
			Vector3 parpos2 = Cam.position;
			
		    Parent.position = parpos2;
			
			Cam.parent = RotationPoint;
			
			x2+=Input.GetAxis("Mouse X") * xSpeed * 0.02f ;
			y2-=Input.GetAxis("Mouse Y") * ySpeed * 0.02f ;
			
			Quaternion rotation = Quaternion.Euler(y2, x2, 0);
	
			RotationPoint.rotation = rotation;
		
		}
		
		
		//--------------------------------------
		
		//Something Important-------------------
		
		if(Input.GetMouseButtonUp(1)){
			
			Quaternion ParRot = Quaternion.Euler(0, x2, 0);
			
			Parent.rotation = ParRot;
			
		}
		
		//--------------------------------------
	}
}

Instructions:
-Cam is obviously the camera, parent and rotation point are empty game objects.
-You need to have a plane for this to work, because the RotationPoint moves on a collider.

So yeah, I hope that I helped somebody :slight_smile:

-Xentarok

P.S.:
If you have any questions or problems write about them below.

P.S.2:
My english is bad, so sorry for the mistakes.

Thanks, this is really helpful, especially for beginners like me