当我在 Unity 2D 中触摸其中一个时,两个不同的 Raycast 2D 同时击中

Two different Raycast 2D are hitting at the same time when i touch one of them in Unity 2D

我创建了一个 raycast 2D 以在单击时销毁第一个 gameObject,并创建了第二个 raycast 2D 以在单击时销毁第二个 gameObject。

当我点击第一个游戏对象时,两个游戏对象同时被销毁,为什么会发生这种情况,我怎样才能让它只销毁我触摸的对象?

// First gameObject Script
using UnityEngine.EventSystems;

RaycastHit2D hit1Up = Physics2D.CircleCast(transform.position, 0.5f, Vector2.up * 0.35f);
RaycastHit2D hit1Bottom = Physics2D.CircleCast(transform.position, 0.5f, Vector2.down * 0.77f);
Debug.DrawRay(transform.position, Vector3.up * 0.35f, Color.green);
Debug.DrawRay(transform.position, Vector3.down * 0.77f, Color.green);

Ray firstRay = Camera.main.ScreenPointToRay(Input.mousePosition);

     if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
     {

         if (hit1Up.collider != null || hit1Bottom.collider != null)
         {
             if (hit1Up.collider.tag == "TagName1" || hit1Bottom.collider.tag == "TagName1")
             {
                 Debug.Log("You touched TagName1");
                 destroy(this.gameObject);
             }
         }

     }


// Second gameObject Script
using UnityEngine.EventSystems;

RaycastHit2D hit2Up = Physics2D.CircleCast(transform.position, 0.5f, Vector2.up * 0.35f);
RaycastHit2D hit2Bottom = Physics2D.CircleCast(transform.position, 0.5f, Vector2.down * 0.77f);
Debug.DrawRay(transform.position, Vector3.up * 0.35f, Color.yellow);
Debug.DrawRay(transform.position, Vector3.down * 0.77f, Color.yellow);

Ray secondRay = Camera.main.ScreenPointToRay(Input.mousePosition);

     if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
     {

         if (hit2Up.collider != null || hit2Bottom.collider != null)
         {
             if (hit2Up.collider.tag == "TagName2" || hit2Bottom.collider.tag == "TagName2")
             {
                 Debug.Log("You touched TagName2");
                 destroy(this.gameObject);
             }
         }

     }

if (mouse is down) { 检查我感兴趣的对象是否存在,然后销毁它}

您的代码没有检查光线 是否确实击中了物体! 或者,就此而言,甚至执行光线投射以查看他的 任何东西。

if (hit2Up.collider != null || hit2Bottom.collider != null)

天哪,我希望在您的代码运行之前这是真的。这个检查没用。

if (hit2Up.collider.tag == "TagName2" || hit2Bottom.collider.tag == "TagName2")

我认为这要么永远正确,要么永远错误。鉴于您的代码正在破坏对象,我怀疑这是真的。这个检查没用。

你应该做什么:

  1. 使用out RaycastHit参数调用Physics.raycast(这将被包装在一个if语句中,如果你没有命中任何东西,不需要执行任何额外的检查)
  2. 检查 hit.collider == hitUp.collider

您也不需要为此使用两个脚本,相同的脚本使用不同的值两次就足够了。

示例实现:

string tagName;

Update() {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if(Physics.Raycast(ray, out hit)) {
        if(hit.collider.CompareTag(tagName)) {
            Debug.Log("You touched " + tagName);
            destroy(hit.collider.gameObject);
        }
    }
}

注意:如果你使用2D物理,你需要分别使用RaycastHit2DPhysics2D。 2D 和 3D 物理引擎不以任何方式通信。

感谢@Draco18s 的回复,它帮助我通过一些研究得出了我需要的 2D 版本逻辑。干杯!

string tagName1, tagName2;

if (Input.GetMouseButtonDown(0))
    {
        Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);

        if (hit.collider != null)
        {
            //Debug.Log(hit.collider.name);
            if (hit.collider.CompareTag(tagName1))
            {
                print(tagName1);
                Destroy(hit.collider.gameObject);
            } else if (hit.collider.CompareTag(tagName2))
            {
                print(tagName2);
                Destroy(hit.collider.gameObject);
            }
        }
    }