Skip to content

Instantly share code, notes, and snippets.

@Crushy
Last active April 4, 2019 10:54
Show Gist options
  • Save Crushy/7761508 to your computer and use it in GitHub Desktop.
Save Crushy/7761508 to your computer and use it in GitHub Desktop.
Pan a camera around in Unity like any modern Strategy game
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class MousePan : MonoBehaviour {
#region Settings
//Default middle mouse
public int mouseKey = 2;
public float sensitivity = 0.1f;
#endregion
#region Private Properties
private Vector3 startDragPos;
private Camera ourCam;
#endregion
// Used for self-initialization
void Awake () {
//Just in case you ever go mobile
Input.simulateMouseWithTouches = true;
}
// Used for reference assignment
void Start() {
ourCam = this.camera;
}
// Update is called once per frame
void Update () {
if ( Input.GetMouseButtonDown( mouseKey ) ) {
startDragPos = Input.mousePosition;
}
if ( Input.GetMouseButton( mouseKey ) ) {
var destination = ourCam.transform.position + (startDragPos - Input.mousePosition);
ourCam.transform.position = Vector3.Lerp( ourCam.transform.position, destination, Time.deltaTime * sensitivity );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment