简体   繁体   English

触摸拖放到目标上

[英]Touch Dragging and Dropping onto Target

I have the following script attached to my game object:我的游戏对象附加了以下脚本:

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");
    }
}

My problem is I dont know how to check that the Collider in gameObject has hit one of the colliders in the dropZones List.我的问题是我不知道如何检查 gameObject 中的碰撞器是否击中了 dropZones 列表中的碰撞器之一。 I dont get any errors, but I don't get the message "Touch: Found Collider" either.我没有收到任何错误,但我也没有收到消息“触摸:找到对撞机”。 So the game doesnt acknoledge that the object has found its target.因此游戏不会确认对象已找到其目标。

What is the best way to go about this?解决这个问题的最佳方法是什么?

I tried doing if(dropZone.GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPosition))我试着做if(dropZone.GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPosition))

Which works to a point, but it does mean that the user can just touch the target without dragging the object and it'll recognize a drop.这在一定程度上是有效的,但这确实意味着用户可以在不拖动对象的情况下触摸目标,并且它会识别出水滴。 With the MouseEvents I have Ondrop, is there an equivilent for Touch?对于我有 Ondrop 的 MouseEvents,Touch 是否有等效项?

To check if a collider hit other collider you can use Collider2D messages like OnCollisionEnter2D , OnCollisionExit2D , OnCollisionStay2D .要检查对撞机是否撞到其他对撞机,您可以使用 Collider2D 消息,如OnCollisionEnter2DOnCollisionExit2DOnCollisionStay2D There are similar messages if your collider is set to trigger like OnTriggerEnter2D .如果您的碰撞器设置为像OnTriggerEnter2D那样触发,则会有类似的消息。

Once you detect the collision check if the collider is in the dropZones list.一旦检测到碰撞,请检查碰撞器是否在 dropZones 列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM