Skip to content

Instantly share code, notes, and snippets.

@Zonciu
Last active December 1, 2022 15:41
Show Gist options
  • Save Zonciu/1389731cd97bf60b223358338873b1ea to your computer and use it in GitHub Desktop.
Save Zonciu/1389731cd97bf60b223358338873b1ea to your computer and use it in GitHub Desktop.
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