i am making a rock paper scissors game but not the classic one that everyone knows, you have 12 cards, 3 blue cards of each type(3 paper cards,3 rock cards,3 scissors cards), 2 purple cards of each type and one last card that contains paper and scissors at the same time and for the game to start you have to arange those cards in slots.
after a lot of thinking i made it work with drag&drop system but i faced some problems:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DragDrop : MonoBehaviour, IPointerDownHandler,IBeginDragHandler,IEndDragHandler,IDragHandler
{
private RectTransform rectTransform;
private CanvasGroup canvasGroup;
public Text textField;
[SerializeField] Canvas canvas;
private Transform originalPos;
private void Awake()
{
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
}
public void OnBeginDrag(PointerEventData eventData)
{
canvasGroup.alpha = .5f;
canvasGroup.blocksRaycasts = false;
originalPos = transform.parent;
transform.SetParent(transform.parent.parent);
}
public void OnDrag(PointerEventData eventData)
{
rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData)
{
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
transform.SetParent(originalPos);
transform.localPosition = Vector3.zero;
if (transform.localPosition == Vector3.zero)
{
textField.enabled = true;
}
}
public void OnPointerDown(PointerEventData eventData)
{
textField.enabled = false;
}
}
and here the slots code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class slots : MonoBehaviour, IDropHandler
{
public GameObject slot;
public void OnDrop(PointerEventData eventData)
{
if(eventData.pointerDrag != null)
{
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
}
}
}
i really appreciate anything you can help me with cause i've been stuck in this for 2 days now.
It looks to me as if you're setting the Transform.parent back to the originalPos there in your OnEndDrag method. You should probably do a MouseToWorldPoint and find the object you're hovering over, then set the parent to that gameObjects transform.