Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Drag And Drop Character Selection Help

Discussion in 'Scripting' started by JacksonTheXtremeGamer, Jul 20, 2020.

  1. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    I need some help. I have done of some drag and drop stuff, but now comes the hard part. I need to make the tokens return a value of one when it's on top of a character. Here is the script that I have for the drag and drop, just to let you guys know what I'm doing:
    Code (CSharp):
    1. public class PlayerOneToken : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
    2. {
    3. [SerializeField] private Canvas canvas;
    4.  
    5.     private RectTransform Token;
    6.  
    7. void Awake()
    8.     {
    9.         Token = GetComponent<RectTransform>();
    10.     }
    11.  
    12. public void OnBeginDrag(PointerEventData eventData)
    13.     {
    14.         Debug.Log("OnBeginDrag");
    15.     }
    16.  
    17.     public void OnDrag(PointerEventData eventData)
    18.     {
    19.         Debug.Log("OnDrag");
    20.         Token.anchoredPosition += eventData.delta / canvas.scaleFactor;
    21.     }
    22.  
    23.     public void OnEndDrag(PointerEventData eventData)
    24.     {
    25.         Debug.Log("OnEndDrag");
    26.     }
    27.  
    28.     public void OnPointerDown(PointerEventData eventData)
    29.     {
    30.         Debug.Log("OnPointerDown");
    31.     }
    32. }
    Now in this code, I want to have a return value of 1, the moment it gets slapped on a character. Any help is appreciated.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Well there is IsPointerOverGameObject if you're looking in the UI space. If not, you'll need to raycast into the scene during OnDrag looking on relevant layers to see if you're hovering what you want.

    Either way, you just confirm you're on or off some object and change the int to whatever you want.