【问题标题】:Touch Dragging and Dropping onto Target触摸拖放到目标上
【发布时间】:2020-02-25 12:19:57
【问题描述】:

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

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&lt;Collider2D&gt;() == Physics2D.OverlapPoint(touchPosition))

这在一定程度上有效,但这确实意味着用户只需触摸目标而不拖动对象,它就会识别下降。对于我有 Ondrop 的 MouseEvents,是否有对应于 Touch 的功能?

【问题讨论】:

    标签: c# unity3d drag-and-drop touch


    【解决方案1】:

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

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

    【讨论】:

    • 呃。我想我一直在以错误的方式思考这个问题。因为教程说要在同一个脚本中完成,所以我完全错过了它的 Collider2D 方面。谢谢,我想我现在知道了。
    猜你喜欢
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    相关资源
    最近更新 更多