触摸拖放到目标上

Touch Dragging and Dropping onto Target

我的游戏对象附加了以下脚本:

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

public class TouchDragDrop : MonoBehaviour
{

    // Components
    private Rigidbody2D myRigidbody2D;
    private CanvasGroup canvasGroup;
    private Collider2D myCollider2D;

    // Drop Zones
    [SerializeField] public List<GameObject> dropZones = new List<GameObject>();

    // Indicators
    private bool isMoving = false;
    private bool debuglog = true;
    public bool isDropped = false;

    // Numbers
    private Vector2 touchPosition;
    private float deltaX, deltaY;
    private Vector2 oldVelocity;

    // Start is called before the first frame update
    void Start()
    {
        myRigidbody2D = GetComponent<Rigidbody2D>();
        myCollider2D = GetComponent<Collider2D>();
        canvasGroup = GetComponent<CanvasGroup>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            // touchPosition = touch.position; 
            touchPosition = Camera.main.ScreenToWorldPoint(touch.position);

            switch (touch.phase)
            {
              case TouchPhase.Began: beginTouchMove();  break;
              case TouchPhase.Moved: moveTouch();  break;
              case TouchPhase.Stationary: stationaryTouch(); break;
              case TouchPhase.Ended: endTouchMove();  break;
            }

        }
    }

    private void beginTouchMove()
    {
        if (myCollider2D == Physics2D.OverlapPoint(touchPosition))
        {
            if(debuglog) Debug.Log("Begin Touch @  x: " + touchPosition.x.ToString() + " y: " + touchPosition.y.ToString());
            //if(debuglog) Debug.Log("RigidBody: " + myRigidbody2D.position.ToString());
            deltaX = touchPosition.x - transform.position.x;
            deltaY = touchPosition.y - transform.position.y;
            isMoving = true;
            myRigidbody2D.bodyType = RigidbodyType2D.Kinematic;
            oldVelocity = myRigidbody2D.velocity;
            myRigidbody2D.velocity = new Vector2(0f, 0f);
        }
    }
    private void moveTouch()
    {
        if (isMoving)
        {
            Vector2 newPosition = new Vector2(touchPosition.x - deltaX, touchPosition.y - deltaY);
            //gameObject.transform.position = newPosition;
            myRigidbody2D.MovePosition(newPosition);
            //if(debuglog) Debug.Log("Touch Position:  x: " + touchPosition.x.ToString() + " y: " + touchPosition.y.ToString());
            //if(debuglog) Debug.Log("RigidBody: " + myRigidbody2D.position.ToString());
        }
    }
    private void endTouchMove()
    {
        if (debuglog) Debug.Log("On End Touch");
        canvasGroup.blocksRaycasts = true;


        // Check drop zones for overlap
        foreach(GameObject dropZone in dropZones)
        {
            if (debuglog) Debug.Log("Checking " + dropZone.name);
            if(dropZone.GetComponent<Collider2D>() == Physics2D.OverlapPoint(myCollider2D.transform.position))
            {
                isDropped = true;
                if (debuglog) Debug.Log("TOUCH: Found Collider");
                dropZone.GetComponent<DropZone>().EndGameObjectLife(gameObject);
            }
        }

        if(!isDropped) { 
            myRigidbody2D.bodyType = RigidbodyType2D.Dynamic;
            myRigidbody2D.velocity = oldVelocity;
        }
    }
    private void stationaryTouch()
    {
        //Debug.Log("Stationary Touch");
    }
}

我的问题是我不知道如何检查 gameObject 中的 Collider 是否击中了 dropZones List 中的一个 collider。我没有收到任何错误,但也没有收到 "Touch: Found Collider" 消息。所以游戏不会确认对象已找到目标。

解决此问题的最佳方法是什么?

我试过 if(dropZone.GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPosition))

这在一定程度上有效,但它确实意味着用户无需拖动对象即可触摸目标并且它会识别掉落。使用 MouseEvents 我有 Ondrop,是否有 Touch 的等效项?

要检查对撞机是否撞击了其他对撞机,您可以使用 Collider2D 消息,例如 OnCollisionEnter2D, OnCollisionExit2D, OnCollisionStay2D. There are similar messages if your collider is set to trigger like OnTriggerEnter2D

检测到碰撞后,检查碰撞器是否在 dropZones 列表中。