2D Tilemap boundary
Hi guys, I'm trying to implement some boundaries for a camera but I'm getting some problems.
I have a camera and a Tilemap 2D, and I did the drag camera using mouse... that its working fine. Then I decided to add some boundaries... to let the camera stick inside the tilemap.
Right now the camera is shaking, is not working at all, I'm not able to drag the camera. I'm not sure what is wrong. Anybody can help me?
Here is what I did so far...
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;
}
}
Comment
Your answer
Follow this Question
Related Questions
Network how to synchronize large tilemap that's made up of bits 0 Answers
Can you use tIDE with Unity? 0 Answers
Get tile from trigger 0 Answers
destructible tilemap, destroying tiles through overlapcircle 1 Answer
Android 2D platformer, low fps 1 Answer