Created
December 24, 2012 06:35
-
-
Save benloong/4368086 to your computer and use it in GitHub Desktop.
Simple RTS Camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class RTSCamera : MonoBehaviour { | |
public float scrollArea = 50f; | |
public float scrollSpeed = 50f; | |
public float rotateSpeed = 4f; | |
// camera look at position | |
Vector3 _followPos; | |
public Vector3 followPos { | |
get { | |
return _followPos; | |
} | |
set { | |
value.x = Mathf.Clamp(value.x, 0, 200); //clamp position | |
value.z = Mathf.Clamp(value.z, 0, 200); | |
_followPos = value; | |
} | |
} | |
Vector3 currentPos; // current camera look at position | |
float angleY = 0f; | |
//height factor | |
float current = 0.5f; //[0,1] | |
float target = 0.5f; //[0,1] | |
float lerpSpeed = 1f; | |
void Start() { | |
followPos = new Vector3(100, 0, 100); | |
} | |
// Update is called once per frame | |
void Update () { | |
HandleInput(); | |
lerpSpeed = Mathf.Lerp(lerpSpeed, 1f, Time.deltaTime * 5); | |
float factor = Mathf.Clamp01(Time.deltaTime*10f*lerpSpeed); | |
currentPos = Vector3.Lerp(currentPos, followPos, factor); | |
float angleX = Mathf.Lerp(30,80,current); | |
Quaternion targetRot = Quaternion.Euler(angleX, angleY,0.0f); | |
Vector3 offset = Vector3.forward * Mathf.Lerp(8f,30f,current); | |
transform.position = Vector3.Lerp(transform.position, currentPos - targetRot*offset, factor); | |
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, factor); | |
} | |
void FixedUpdate() { | |
RaycastHit hit; | |
_followPos.y = 0; | |
if(Physics.Raycast(followPos+1000*Vector3.up, -Vector3.up, out hit, 1000f, 1<<LayerMask.NameToLayer("Terrain"))) { | |
_followPos.y = hit.point.y; | |
} | |
} | |
void HandleInput() { | |
float posx = Input.mousePosition.x; | |
float posy = Input.mousePosition.y; | |
float factor = Mathf.Min(1.0f, Time.deltaTime * 10f); | |
target = Mathf.Clamp01(target - Input.GetAxis("Mouse ScrollWheel") * 0.25f); | |
current = current * (1.0f - factor) + target * factor; | |
if(Input.GetMouseButton(1)) { | |
angleY += Input.GetAxis("Mouse X") * rotateSpeed ; | |
} | |
else { | |
Vector3 forward = transform.forward; | |
Vector3 right = transform.right; | |
forward.y = 0; | |
right.y = 0; | |
forward.Normalize(); | |
right.Normalize(); | |
if (posx < scrollArea) {followPos += -scrollSpeed *right* Time.deltaTime;} | |
if (posx >= Screen.width-scrollArea) {followPos += scrollSpeed * right*Time.deltaTime;} | |
if (posy < scrollArea) {followPos += -forward * scrollSpeed * Time.deltaTime;} | |
if (posy >= Screen.height-scrollArea) {followPos += forward*scrollSpeed * Time.deltaTime;} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment