Last active
December 1, 2022 15:41
-
-
Save Zonciu/1389731cd97bf60b223358338873b1ea to your computer and use it in GitHub Desktop.
Unity mouse drag move camera, from https://forum.unity.com/threads/click-drag-camera-movement.39513/#post-1939243
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
public class CameraDragMove : MonoBehaviour | |
{ | |
private Vector3 ResetCamera; | |
private Vector3 Origin; | |
private Vector3 Diference; | |
private bool Drag = false; | |
void Start() | |
{ | |
ResetCamera = Camera.main.transform.position; | |
} | |
void LateUpdate() | |
{ | |
if (Input.GetMouseButton(0)) | |
{ | |
Diference = (Camera.main.ScreenToWorldPoint(Input.mousePosition)) - Camera.main.transform.position; | |
if (Drag == false) | |
{ | |
Drag = true; | |
Origin = Camera.main.ScreenToWorldPoint(Input.mousePosition); | |
} | |
} | |
else | |
{ | |
Drag = false; | |
} | |
if (Drag == true) | |
{ | |
Camera.main.transform.position = Origin - Diference; | |
} | |
//RESET CAMERA TO STARTING POSITION WITH RIGHT CLICK | |
if (Input.GetMouseButton(1)) | |
{ | |
Camera.main.transform.position = ResetCamera; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment