Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class DraggableBlock : MonoBehaviour
- {
- private bool isDragging = false;
- private Vector2 startPosition;
- private GridManager gridManager;
- private void Start()
- {
- gridManager = FindObjectOfType<GridManager>();
- if (gridManager == null)
- {
- Debug.LogError("❌ GridManager NOT found in the scene! Make sure GridManager exists.");
- }
- }
- private void OnMouseDown()
- {
- startPosition = transform.position; // Store the original position
- isDragging = true;
- }
- private void OnMouseDrag()
- {
- if (!isDragging) return;
- Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- transform.position = mousePosition; // Move block with the mouse
- }
- private void OnMouseUp()
- {
- isDragging = false;
- if (gridManager == null)
- {
- Debug.LogError("❌ GridManager reference is missing!");
- return;
- }
- Vector2 snappedPosition = gridManager.SnapToGrid(transform.position);
- if (gridManager.IsCellEmpty(snappedPosition))
- {
- transform.position = snappedPosition;
- gridManager.PlaceBlock(snappedPosition, transform);
- Debug.Log($"✅ Block successfully placed at: {snappedPosition}");
- }
- else
- {
- Debug.LogWarning("⚠️ Invalid Placement: Returning to start position.");
- transform.position = startPosition;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement