I am trying to create a simple RTS camera that scrolls when the mouse is at the edges of the screen, here is my code:
public float scrollSpeed = 5.0F;
public float scrollBuffer = 10.0F;
public float damping = 2.0F;
private Transform _tr;
void Awake () {
_tr = transform;
}
void Update () {
Vector3 newPos = _tr.position;
if (Input.mousePosition.x >= Screen.width-scrollBuffer)
newPos.x = Mathf.Lerp (_tr.position.x, _tr.position.x + scrollSpeed, damping * Time.deltaTime);
if (Input.mousePosition.x <= scrollBuffer)
newPos.x = Mathf.Lerp (_tr.position.x, _tr.position.x - scrollSpeed, damping * Time.deltaTime);
if (Input.mousePosition.y >= Screen.height-scrollBuffer)
newPos.z = Mathf.Lerp (_tr.position.z, _tr.position.z + scrollSpeed, damping * Time.deltaTime);
if (Input.mousePosition.y <= scrollBuffer)
newPos.z = Mathf.Lerp (_tr.position.z, _tr.position.z - scrollSpeed, damping * Time.deltaTime);
_tr.position = newPos;
}
I want the camera to come to a smooth halt when the mouse moves away from the screen bounds, but the “damping” method I have implemented above doesn’t seem to work. Does anyone know what I am doing wrong?