Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Tilemaps;
- public class PanMap : MonoBehaviour
- {
- private Vector3 touchStart;
- public Bounds tilemapBounds;
- public Vector2 screenBounds;
- private void Start()
- {
- var obj = GameObject.Find("Tilemap_Ground").GetComponentInChildren<TilemapRenderer>();
- tilemapBounds = obj.bounds;
- screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
- }
- private void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- }
- if (Input.GetMouseButton(0))
- {
- Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
- Camera.main.transform.position += direction;
- }
- }
- private void LateUpdate()
- {
- Vector3 viewPos = transform.position;
- viewPos.x = Mathf.Clamp(viewPos.x, (screenBounds.x / 2) + tilemapBounds.min.x, (screenBounds.x / 2) - tilemapBounds.max.x);
- viewPos.y = Mathf.Clamp(viewPos.y, (screenBounds.y / 2) + tilemapBounds.min.y / 2, (screenBounds.y / 2) - tilemapBounds.max.y);
- viewPos.z = transform.position.z;
- transform.position = viewPos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement