Last active
May 22, 2019 22:38
-
-
Save wackoisgod/f23faaa95c6db221db0ef887fff40454 to your computer and use it in GitHub Desktop.
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Tilemaps; | |
public class CameraMover : MonoBehaviour | |
{ | |
public Tilemap map; | |
public Grid grid; | |
public int cameraMarin = 50; | |
public int cameraSpeed = 200; | |
public int cameraZoomLimit = 200; | |
public bool supportEdgeScrolling = true; | |
public bool supportKeyScrolling = true; | |
public bool supportMouseDragScrolling = true; | |
public Vector2 cameraMovement = Vector2.zero; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
var screenSize = Screen.currentResolution; | |
var mousePosition = Input.mousePosition; | |
var mousePositionScreen = Camera.main.ScreenToViewportPoint(mousePosition); | |
if (supportEdgeScrolling) | |
{ | |
if (screenSize.width - mousePosition.x <= cameraMarin) | |
cameraMovement.x += cameraSpeed * Time.deltaTime; | |
if (mousePosition.x <= cameraMarin) | |
cameraMovement.x -= cameraSpeed * Time.deltaTime; | |
if (screenSize.height - mousePosition.y <= cameraMarin) | |
cameraMovement.y += cameraSpeed * Time.deltaTime; | |
if (mousePosition.y <= cameraMarin) | |
cameraMovement.y -= cameraSpeed * Time.deltaTime; | |
} | |
if (supportKeyScrolling) | |
{ | |
if (Input.GetKeyDown(KeyCode.LeftArrow)) | |
{ | |
cameraMovement.x -= cameraSpeed * Time.deltaTime; | |
} | |
if (Input.GetKeyDown(KeyCode.UpArrow)) | |
{ | |
cameraMovement.y -= cameraSpeed * Time.deltaTime; | |
} | |
} | |
gameObject.transform.position += (Vector3)cameraMovement; | |
cameraMovement = Vector2.zero; | |
if (Input.GetMouseButton(0)) | |
{ | |
var mouseWorldPosition = Camera.main.ScreenToWorldPoint(mousePosition); | |
var tt = grid.WorldToCell(mousePosition); | |
Debug.Log(tt); | |
map.SetTile(Vector3Int.zero, null); | |
} | |
} | |
void FixedUpdate() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment