简体   繁体   English

Unity3D使用OnTriggerStay

[英]Unity3D using OnTriggerStay

I'm using the event OnTriggerStay2D to destroy an object, the reason i'm using this instead of OnTriggerEnter2D is because i'm dragging the object using the touchscreen, and i want to destroy it after it is released, the problem i have is that OntriggerStay2D is not always called, so sometimes the object is not destroyed after it is released and it has to be moved again to work, i've read the docummentation from Unity 我正在使用事件OnTriggerStay2D来销毁一个对象,我使用它而不是OnTriggerEnter2D的原因是因为我使用触摸屏拖动对象,我想在它被释放后销毁它,我的问题是并不总是调用OntriggerStay2D,因此有时对象在释放后不会被销毁,并且必须再次移动才能工作,我已经从Unity中读取了文档

OnTriggerStay is called almost all the frames for every Collider other that is touching the trigger. 几乎所有碰撞触发器的碰撞器都会调用OnTriggerStay的所有帧。

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

            Destroy (other.gameObject);

    }
}

I would like to know if there's any way to call OntriggerStay2D everytime i release the object. 我想知道每次释放对象时是否有任何方法可以调用OntriggerStay2D。 Thanks. 谢谢。

Edit Dragging code 编辑拖动代码

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;     

        }

    }

Avoid using OnTriggerStay2D for this. 避免使用OnTriggerStay2D You can use a boolean variable that you set to true and false in the OnTriggerEnter2D and OnTriggerExit2D function. 您可以在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;
    }
}

You can now check the isTouching variable when the object is released. 现在,您可以在释放对象时检查isTouching变量。

if(isTouching){
....
}

Note that I suggest you abandon your current code that uses Raycast and Input.GetMouseButton(0); 请注意,我建议您放弃使用Raycast和Input.GetMouseButton(0);当前代码Input.GetMouseButton(0); since you are using this on mobile devices too. 因为你也在移动设备上使用它。 You should be using Unity's new EventSystem for this since it is made to be mobile friendly too. 你应该使用Unity的新EventSystem,因为它也是适合移动设备的。

Since you are using 2D collider, see #7 from this answer. 由于您使用的是2D对撞机,请参阅答案中的#7

Here is a complete example of how to drag a Sprite with the new EventSystem. 是一个如何使用新的EventSystem拖动Sprite的完整示例。 Combine that with the answer above and you get a much more better solution. 将其与上述答案相结合,您将获得更好的解决方案。

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

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