Last active
April 4, 2019 10:54
-
-
Save Crushy/7761508 to your computer and use it in GitHub Desktop.
Pan a camera around in Unity like any modern Strategy game
This file contains 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; | |
[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