so, I am basically making a puzzle game where a big shape(in black) is given and the player needs to fill that shape/shadow with different smaller sized shapes.
For 1st level, I made it so only a specific shape can be placed at the positions, but now for level 2, I need to make it so that any shape can be placed anywhere and it’s correct as long as the whole black shape is covered.
Level 1:
So as you can see I am showing a reference image on the left side which the player needs to copy.
for Level 2:
For Level 2 I need something like this where the user can put any shape anywhere on the black, and the condition for correct is that it needs to cover the whole area and should not go out of the black.
Currently, I have this code for my drag and drop script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Tess1DragDrop : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler {
[SerializeField] private Canvas canvas;
private RectTransform rectTransform;
public Vector3 startPos;
private CanvasGroup canvasGroup;
private GameObject onShadow;
private GameObject onPanel;
Camera cam;
private Vector3 CurrentPos;
public bool atPlace;
private void Awake() {
onShadow = GameObject.FindGameObjectWithTag("onShadow");
onPanel = GameObject.FindGameObjectWithTag("onPanel");
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
startPos= rectTransform.anchoredPosition;
cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
}
public void Update(){
}
public void OnBeginDrag(PointerEventData eventData) {
// Debug.Log("OnBeginDrag");
eventData.pointerDrag.transform.SetParent(onShadow.transform);
canvasGroup.alpha = .6f;
canvasGroup.blocksRaycasts = false;
this.tag = "Untagged";
}
public void OnDrag(PointerEventData eventData) {
//Debug.Log("OnDrag");
rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData) {
// Debug.Log("OnEndDrag");
if(eventData.pointerDrag.tag!="Panel"){
CurrentPos = cam.WorldToViewportPoint(transform.position);
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
if(rectTransform.anchoredPosition.y < -125f){
eventData.pointerDrag.transform.SetParent(onPanel.transform);
rectTransform.anchoredPosition = startPos;
}
if(rectTransform.anchoredPosition.y > -125f){
//eventData.pointerDrag.transform.SetParent(onShadow.transform);
StartCoroutine(moveBack());
}
if(CurrentPos.x > 0.9f){
eventData.pointerDrag.transform.SetParent(onPanel.transform);
rectTransform.anchoredPosition = startPos;
}
else if(CurrentPos.x < 0.1f){
eventData.pointerDrag.transform.SetParent(onPanel.transform);
rectTransform.anchoredPosition = startPos;
}
else if(CurrentPos.y > 1f){
eventData.pointerDrag.transform.SetParent(onPanel.transform);
rectTransform.anchoredPosition = startPos;
}}
}
IEnumerator moveBack(){
yield return new WaitForSeconds(0.01f);
if(gameObject.tag != "atPlace"){
gameObject.transform.SetParent(onPanel.transform);
gameObject.GetComponent<RectTransform>().anchoredPosition = startPos;
gameObject.tag = "Untagged";
gameObject.GetComponent<Image>().raycastTarget = true;
}
}
public void OnPointerDown(PointerEventData eventData) { //imp method
// Debug.Log("OnPointerDown");
}
}
and for the item slot script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Tess1ItemSlot : MonoBehaviour, IDropHandler {
private GameObject onShadow;
private GameObject onPanel;
//public bool atPlace;
public void Start(){
onShadow = GameObject.FindGameObjectWithTag("onShadow");
onPanel = GameObject.FindGameObjectWithTag("onPanel");
// atPlace = false;
}
public void OnDrop(PointerEventData eventData) {
// Debug.Log("OnDrop");
if(eventData.pointerDrag.tag!="Panel"){
if (eventData.pointerDrag != null && eventData.pointerDrag.GetComponent<Image>().sprite == gameObject.GetComponent<Image>().sprite) {
eventData.pointerDrag.transform.SetParent(onShadow.transform);
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
eventData.pointerDrag.tag = "atPlace";
eventData.pointerDrag.GetComponent<Image>().raycastTarget = false;
// atPlace = true;
}
if(eventData.pointerDrag.GetComponent<Image>().sprite != gameObject.GetComponent<Image>().sprite){
// Debug.Log("NotSame");
eventData.pointerDrag.transform.SetParent(onPanel.transform);
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = eventData.pointerDrag.GetComponent<Tess1DragDrop>().startPos;
eventData.pointerDrag.tag = "Untagged";
eventData.pointerDrag.GetComponent<Image>().raycastTarget = true;
// atPlace = false;
}
}
}
}