I’m an extremely new coder, but I feel like some people could benefit from my little camera script. Believe it or not, I’ve actually spent around 15 hours trying to get a working camera controller, and after many iterations I created this.
This scrolls smoothly, uses both the mouse and the keyboard, and can zoom in or out using the mouse wheel.
There are also camera constrains, so that the camera can not escape the bounds of a rectangular map.
I would appreciate any suggestions for improvement, and if you find it useful yourself just mention it in the comments! I don’t think this is nearly complicated enough to be worth selling, but I hope it makes someone happy
/*
RTS Camera Controller
By Wyko ter Haar
Moves the camera within the bounds of the map variables.
An origin point of 0,0 is assumed.
*/
// The camera bounds
var mapMinX : int;
var mapMinZ : int;
var mapMaxX : int;
var mapMaxZ : int;
// Zoom limits for the camera
var mapMaxY : int = 9.5;
var mapMinY : int = 4;
var scrollArea = 7; // Defines the distance from the edge of the window that mouse scrolling starts
var scrollSpeed = 22; // Defines how fast the window scrolls
// Translates the camera
function moveMe(myDir, mySpeed) {
switch (myDir)
{
case ("Left") :
myVector = (Vector3(mySpeed,0,0) * scrollSpeed * Time.deltaTime);
break;
case ("Right") :
myVector = (Vector3(mySpeed,0,0) * scrollSpeed * Time.deltaTime);
break;
case ("Forwards") :
myVector = (Vector3(0,0,mySpeed) * scrollSpeed * Time.deltaTime);
break;
case ("Backwards") :
myVector = (Vector3(0,0,mySpeed) * scrollSpeed * Time.deltaTime);
break;
case ("Up") :
myVector = (Vector3(0,mySpeed,0));
break;
case ("Down") :
myVector = (Vector3(0,mySpeed,0));
break;
default : Debug.Log("Can't Move.");
}
if (InBounds(myVector))
{transform.Translate(myVector, Space.World);}
}
function Update () {
var mPosX = Input.mousePosition.x;
var mPosY = Input.mousePosition.y;
// Do camera movement by mouse position
if (mPosX < scrollArea) {moveMe("Left", -1);}
if (mPosX >= Screen.width-scrollArea) {moveMe("Right", 1);}
if (mPosY >= Screen.height-scrollArea) {moveMe("Forwards", 1);}
if (mPosY < scrollArea) {moveMe("Backwards", -1);}
// Do camera movement by keyboard
if (Input.GetAxis("Horizontal") < 0) {moveMe("Left", Input.GetAxis("Horizontal"));}
if (Input.GetAxis("Horizontal") > 0) {moveMe("Right", Input.GetAxis("Horizontal"));}
if (Input.GetAxis("Vertical") > 0) {moveMe("Forwards", Input.GetAxis("Vertical"));}
if (Input.GetAxis("Vertical") < 0) {moveMe("Backwards", Input.GetAxis("Vertical"));}
// Zoom Camera in or out
if (Input.GetAxis("Mouse ScrollWheel") < 0) {
moveMe("Up", .2);
}
if (Input.GetAxis("Mouse ScrollWheel") > 0) {
moveMe("Down", -.2);
}
}
// Checks to see if the camera would be in bounds after the move
// if not, it brings the camera back to the edge of the bounds
function InBounds (vector : Vector3) : boolean {
var answer : boolean = true;
if ((transform.position.x + vector.x) < mapMinX) {
transform.position.x = mapMinX;
answer = false;
}
if ((transform.position.z + vector.z) < mapMinZ) {
transform.position.z = mapMinZ;
answer = false;
}
if ((transform.position.x + vector.x) > mapMaxX) {
transform.position.x = mapMaxX;
answer = false;
}
if ((transform.position.z + vector.z) > mapMaxZ) {
transform.position.z = mapMaxZ;
answer = false;
}
if ((transform.position.y + vector.y) > mapMaxY) {
transform.position.y = mapMaxY;
answer = false;
}
if ((transform.position.y + vector.y) < mapMinY) {
transform.position.y = mapMinY;
answer = false;
}
return answer;
}