Skip to content

Instantly share code, notes, and snippets.

@wackoisgod
Last active May 22, 2019 22:38
Show Gist options
  • Save wackoisgod/f23faaa95c6db221db0ef887fff40454 to your computer and use it in GitHub Desktop.
Save wackoisgod/f23faaa95c6db221db0ef887fff40454 to your computer and use it in GitHub Desktop.
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