Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Tilemaps;
  3.  
  4. public class PanMap : MonoBehaviour
  5. {
  6.     private Vector3 touchStart;
  7.  
  8.     public Bounds tilemapBounds;
  9.     public Vector2 screenBounds;
  10.  
  11.     private void Start()
  12.     {
  13.         var obj = GameObject.Find("Tilemap_Ground").GetComponentInChildren<TilemapRenderer>();
  14.  
  15.         tilemapBounds = obj.bounds;
  16.         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
  17.     }
  18.  
  19.     private void Update()
  20.     {
  21.         if (Input.GetMouseButtonDown(0))
  22.         {
  23.             touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  24.         }
  25.  
  26.         if (Input.GetMouseButton(0))
  27.         {
  28.             Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
  29.             Camera.main.transform.position += direction;
  30.         }
  31.     }
  32.  
  33.     private void LateUpdate()
  34.     {
  35.         Vector3 viewPos = transform.position;
  36.  
  37.         viewPos.x = Mathf.Clamp(viewPos.x, (screenBounds.x / 2) + tilemapBounds.min.x, (screenBounds.x / 2) - tilemapBounds.max.x);
  38.         viewPos.y = Mathf.Clamp(viewPos.y, (screenBounds.y / 2) + tilemapBounds.min.y / 2, (screenBounds.y / 2) - tilemapBounds.max.y);
  39.         viewPos.z = transform.position.z;
  40.  
  41.         transform.position = viewPos;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement