0
\$\begingroup\$

In my game I am trying to have numbered blocks and math operators be able to be dragged down into their respectively color coded holders (white holders = Numbers, black holders = operations). The spawn in color coded blocks called spawners. If you drop a block you were dragging in a space that isn't a holder, then the block would immediately snap back to its respective spawner.

enter image description here However, I am now encountering this issue where 99% of the time when I drop the block onto its respective holder, the block snaps back to its spawner instead of staying there.

Here's the minimum reproducible code for dragging the blocks:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

[RequireComponent(typeof(Collider2D))]
public class DragDrop : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    [SerializeField] private SpriteRenderer _spriteRenderer;
    public SpriteRenderer _SpriteRenderer => this._spriteRenderer;
    [HideInInspector]public static bool isInSpace;
    [HideInInspector]public Vector3 initialPos;

    private void Start()
    {
        initialPos = transform.position;
        isInSpace = true;
    }

    public void OnDrag(PointerEventData eventData)
    {

        if (isInSpace == false)
        {
            Vector3 movement = new Vector3(
            x: (eventData.delta.x/3) * Time.deltaTime,
            y: (eventData.delta.y/3) * Time.deltaTime);

            this.transform.localPosition += movement;
        }
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        isInSpace = false;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (isInSpace == false)
        {
            transform.position = initialPos;
        }
    }

#if UNITY_EDITOR
    private void Reset()
    {
        this._spriteRenderer = this.GetComponent<SpriteRenderer>();
    }
#endif
}

And Here's the code for receiving the blocks:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class RecieveBlock : MonoBehaviour, IDropHandler
{
    public void OnDrop(PointerEventData Block)
    {
        if (Block.pointerDrag != null && Block.pointerDrag.tag == gameObject.tag)
        {
            Block.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
            DragDrop.isInSpace = true;
        }
    }
        
}

I have no idea why it doesn't set off the isInSpace bool to be true and stay there.

Heirarchy Image For Number Blocks: enter image description here

Heirarchy Image For Operation Blocks: enter image description here

Heirarchy Image For Holder Blocks: enter image description here

Thank you for reading.

\$\endgroup\$
  • \$\begingroup\$ What does this debugging return? gist.github.com/jimu/fb85aa15c458934a9e79faf40857cef3 \$\endgroup\$ – Jim U Aug 10 '20 at 6:37
  • \$\begingroup\$ For Number Blocks: Block.pointerDrag=NOT-NULL, Block.pointerDrag.tag='Number', gameObject.tag='Number'. For Operation Blocks: Block.pointerDrag=NOT-NULL, Block.pointerDrag.tag='Operation', gameObject.tag='Operation' \$\endgroup\$ – kayra yorulmaz Aug 10 '20 at 9:07
0
\$\begingroup\$

OnDrop takes the courser position. Changing the OnDrag event with this code (found in the docu), you will notice that as long as the mouse is over the destination, it is working. But not if the shapes are overlapping, the mouse is important.

public void OnDrag(PointerEventData eventData)
{
    if (isInSpace == false)
    {
        Ray ray = Camera.main.ScreenPointToRay(eventData.position);
        //Calculate the distance between the Camera and the GameObject, and go this distance along the ray
        Vector3 rayPoint = ray.GetPoint(Vector3.Distance(transform.position, Camera.main.transform.position));
        //Move the GameObject when you drag it
        transform.position = rayPoint;
    }
}

You could change it with a collision event that changes your bool isInSpace to true and snap it to the destination on OnEndDrag.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.