- Home /
How to create a Drag & Drop system which verifies what gameObject was dragged into the slot?
I'm really struggling to create a decent Drag & Drop system. The game idea is that the player would drag images (at the left side of the screen) to different slots (at the center) to create an allegorical car, and then the game would give a score to what the player created.
But the problem is: the gameObjects have BoxCollider2D components (both of them have the IsTrigger on) and they don't interact with each other. The item slot doesn't detect that something has been dragged into it and which image was dropped.
This is the script of the gameObjects that are being dragged:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DragDrop : MonoBehaviour, IPointerDownHandler, IPointerClickHandler,
IPointerUpHandler, IPointerExitHandler, IPointerEnterHandler,
IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler {
[SerializeField] private Canvas canvas;
private RectTransform rectTransform;
private CanvasGroup canvasGroup;
float spawnPositionX;
float spawnPositionY;
float spawnPositionZ;
float scaleX;
bool dragging = false;
private void Awake() {
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
spawnPositionX = transform.position.x;
spawnPositionY = transform.position.y;
spawnPositionZ = transform.position.z;
scaleX = transform.localScale.x;
}
public void OnBeginDrag(PointerEventData eventData) {
Debug.Log("Drag begin!");
canvasGroup.alpha = .6f;
canvasGroup.blocksRaycasts = false;
dragging = true;
}
public void OnDrag(PointerEventData eventData) {
Debug.Log("Dragging.");
Vector3 screenPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, canvas.planeDistance);
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
transform.position = worldPosition;
}
public void OnEndDrag(PointerEventData eventData) {
Debug.Log("Drag ended!");
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
dragging = false;
}
public void OnDrop(PointerEventData eventData) {
Debug.Log("OnDrop");
}
public void Update() {
if (dragging) {
if (Input.GetKeyDown(KeyCode.R)) {
scaleX = -scaleX;
this.gameObject.transform.localScale = new Vector3(scaleX, 1f, 1f);
}
}
}
void OnTriggerEnter2D(Collider2D other){
Debug.Log("Test 1.");
if(other.gameObject.tag == "GameSlot"){
Debug.Log("Test 2.");
GameObject iconClone = Instantiate(gameObject, new Vector3(spawnPositionX, spawnPositionY, spawnPositionZ), Quaternion.identity, GameObject.FindWithTag("Canvas").transform);
}else{
Destroy(this);
}
}
And this is the script of the slots which the images will be dragged into:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ItemSlot : MonoBehaviour, IDropHandler {
public void OnDrop(PointerEventData eventData) {
Debug.Log("OnDrop");
if (eventData.pointerDrag != null) {
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
gameObject.GetComponent<Image>().color = new Color32(255,0,0,100);
}
}
How do I solve this problem?
Your answer
Follow this Question
Related Questions
Drag and drop on object to bounce back to it place unity c# 1 Answer
Clicking on one object to destroy a specific number of tagged objects dragged sidebyside 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to add a score when the correct object drags to the correct box? 1 Answer