触摸拖放到目标上>>

问题描述 投票:0回答:1

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

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中的对撞机是否击中了dropZones列表中的对撞机之一。我没有收到任何错误,但也没有收到“触摸:找到对撞机”的消息。因此,游戏无法确认对象已找到其目标。

最佳方法是什么?

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

这可以起到一定的作用,但这确实意味着用户可以直接触摸目标而无需拖动对象,并且它会识别出下落。我拥有Ondrop的MouseEvent,是否有Touch的对等物?

我在游戏对象上附加了以下脚本:使用系统;使用System.Collections;使用System.Collections.Generic;使用UnityEngine;公共类TouchDragDrop:MonoBehaviour {...

c# unity3d drag-and-drop touch
1个回答
1
投票

要检查对撞机是否撞到其他对撞机,可以使用Collider2D消息,例如OnCollisionEnter2DOnCollisionExit2DOnCollisionStay2D。如果将对撞机设置为像OnTriggerEnter2D一样触发,也会有类似的消息。

© www.soinside.com 2019 - 2024. All rights reserved.