簡體   English   中英

Unity3D使用OnTriggerStay

[英]Unity3D using OnTriggerStay

我正在使用事件OnTriggerStay2D來銷毀一個對象,我使用它而不是OnTriggerEnter2D的原因是因為我使用觸摸屏拖動對象,我想在它被釋放后銷毀它,我的問題是並不總是調用OntriggerStay2D,因此有時對象在釋放后不會被銷毀,並且必須再次移動才能工作,我已經從Unity中讀取了文檔

幾乎所有碰撞觸發器的碰撞器都會調用OnTriggerStay的所有幀。

public void OnTriggerStay2D(Collider2D other)
{
        if (gameObject.tag == other.tag) {

            Destroy (other.gameObject);

    }
}

我想知道每次釋放對象時是否有任何方法可以調用OntriggerStay2D。 謝謝。

編輯拖動代碼

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

    public class Drag : MonoBehaviour {

        private bool draggingItem = false;
        private GameObject draggedObject;
        private Vector2 touchOffset;

        void Update ()
        {
            if (HasInput)
            {
                DragOrPickUp();
            }
            else
            {
                if (draggingItem)
                    DropItem();
            }
        }

        Vector2 CurrentTouchPosition
        {
            get
            {
                Vector2 inputPos;
                inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                return inputPos;
            }
        }

        private void DragOrPickUp()
        {

            var inputPosition = CurrentTouchPosition;

            if (draggingItem)
            {

                draggedObject.transform.position = inputPosition + touchOffset;

            }
            else
            {

                RaycastHit2D[] touches = Physics2D.RaycastAll(inputPosition, inputPosition, 0.5f);
                if (touches.Length > 0)
                {
                    var hit = touches[0];
                    if (hit.transform != null && hit.rigidbody != null)
                    {
                        draggingItem = true;
                        draggedObject = hit.transform.gameObject;
                        touchOffset = (Vector2)hit.transform.position - inputPosition;
                    }
                }
            }
        }

        private bool HasInput
        {
            get
            {

                return Input.GetMouseButton(0);
            }
        }

        public void DropItem()
        {
            draggingItem = false;     

        }

    }

避免使用OnTriggerStay2D 您可以在OnTriggerEnter2DOnTriggerExit2D函數中使用設置為true和false的布爾變量。

bool isTouching = false;

void OnTriggerEnter2D(Collider2D collision)
{
    Debug.Log("Entered");
    if (collision.gameObject.CompareTag("YourOtherObject"))
    {
        isTouching = true;
    }
}

void OnTriggerExit2D(Collider2D collision)
{
    Debug.Log("Exited");
    if (collision.gameObject.CompareTag("YourOtherObject"))
    {
        isTouching = false;
    }
}

現在,您可以在釋放對象時檢查isTouching變量。

if(isTouching){
....
}

請注意,我建議您放棄使用Raycast和Input.GetMouseButton(0);當前代碼Input.GetMouseButton(0); 因為你也在移動設備上使用它。 你應該使用Unity的新EventSystem,因為它也是適合移動設備的。

由於您使用的是2D對撞機,請參閱答案中的#7

是一個如何使用新的EventSystem拖動Sprite的完整示例。 將其與上述答案相結合,您將獲得更好的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM