0

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.here an image from the game after a lot of thinking i made it work with drag&drop system but i faced some problems:

  • first problem that everytime i try to drop any card into the slot it returns to it's original position .
  • second problem that i want the slot to become the parent of the card inserted in it but it just leave it's parent when i start dragging it then when i release it it returns to it's original parent(i even tried to delete some lines in my script to test if the second problem is caused by the first one or not,but it's the same even thought that the card get implemented in the slot and those codes aren't for nothing they return the card to it's original position if it's not implemented in a slot ). here the cards code:
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.

1 Answers1

0

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.

  • isn't MouseToWorldPoint related to the camera ? i mean my entire game is inside a canvas i don't know if this will work or no – fadi zarrad Mar 26 '22 at 01:02
  • Whether or not it's a canvas, you're still using a camera to render the image on a screen for the user, so in combination with something like RectangleContainsScreenPoint(RectTransform rect, Vector2 screenPoint, Camera cam); it should work. In either case, the answer to your two questions are that you are setting the Transform.parent back to the orignalPos, then setting it's local position to 0, which is of course the same position as the parent, which is the origanl parent. You need to parent the dropped card to the NEW transform in your OnEndDrag() method. – Eric Bramble Mar 26 '22 at 01:45