Hello, I was messing with RTS cameras and used some of the code here, My camera stops after a certain distance from the center, has zoom using the mouse wheel and also has keyboard controls for arrow keys to use the camera.
On top of these features, zooming really far out no longer slows the cameras movement or zooming (when zoomed out, trying to scroll in any direction or zoom back in was really slow).
Also! And this ones a big one~ the camera moves in world space, so all you need to do is move the camera how high you want it and what angle you want it (could be isometric) and it wont move into the terrain. I //commented some things so people dont get lost. Code below is in C#
using UnityEngine;
using System.Collections;
public class RTSCamera : MonoBehaviour
{
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float mousePosX = Input.mousePosition.x;
float mousePosY = Input.mousePosition.y;
int scrollDistance = 5;
float scrollSpeed = 2 * Camera.main.orthographicSize + 2;
Vector3 aPosition = new Vector3(0, 0, 0);
float ScrollAmount = scrollSpeed *Time.deltaTime;
const float orthographicSizeMin = 15f;
const float orthographicSizeMax = 256f;
//mouse left
if ((mousePosX < scrollDistance) && (transform.position.x > -240))
{
transform.Translate (-ScrollAmount,0,0, Space.World);
}
//mouse right
if ((mousePosX >= Screen.width - scrollDistance) && (transform.position.x < 240))
{
transform.Translate (ScrollAmount,0,0, Space.World);
}
//mouse down
if ((mousePosY < scrollDistance) && (transform.position.z > -240))
{
transform.Translate (0,0,-ScrollAmount, Space.World);
}
//mouse up
if ((mousePosY >= Screen.height - scrollDistance) && (transform.position.z < 240))
{
transform.Translate (0,0,ScrollAmount, Space.World); ;
}
//Keyboard controls
if ((Input.GetKey(KeyCode.UpArrow)) && (transform.position.z < 240))
{
transform.Translate (0,0,ScrollAmount, Space.World); ;
}
if ((Input.GetKey(KeyCode.DownArrow)) && (transform.position.z > -240))
{
transform.Translate (0,0,-ScrollAmount, Space.World);
}
if ((Input.GetKey(KeyCode.LeftArrow)) && (transform.position.x > -240))
{
transform.Translate (-ScrollAmount,0,0, Space.World);
}
if ((Input.GetKey(KeyCode.RightArrow)) && (transform.position.x < 240))
{
transform.Translate (ScrollAmount,0,0, Space.World);
}
//Scrolling Zoom
if (Input.GetAxis("Mouse ScrollWheel") < -0) // forward
{
Camera.main.orthographicSize *= 1.1f;
}
if (Input.GetAxis("Mouse ScrollWheel") > -0) // back
{
Camera.main.orthographicSize *= 0.9f;
}
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, orthographicSizeMin, orthographicSizeMax );
}
}