如何检测在 Unity 中手动添加的线渲染器的碰撞器上的触摸?

How to detect touch on colliders for a line renderer added manually in Unity?

我通过在 line.I 周围实例化手动为线渲染器添加了一个胶囊碰撞器,我已经添加了不同的精灵到这个 scene.I 使用 [=21 检测这些精灵顶部的触摸=]Physics2D Raycaster 在 IPointerEnterHandler 的帮助下在主相机和脚本中将它们放入包含精灵 renderer.Sample 代码的所需游戏对象中 renderer.Sample 下面给出的代码

 public class TouchDetect:MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
    {

     public void OnPointerEnter(PointerEventData eventData)
      {
        if (eventData.pointerEnter.gameObject.name == "redcircle2(Clone)" )
            {
                sound[1].source.clip = sound[1].clip;
                sound[1].source.loop = true;
                sound[1].source.Play();
            }
       }

    }

我添加的胶囊碰撞器是 3d collider.How 来检测碰撞器顶部的触摸以及 2d 碰撞器?

我试过下面的代码,但没有用。

     if (Input.touchCount > 0 || Input.GetMouseButton(0))
      {
       Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
       bool collided = this.GetComponent<CapsuleCollider>().bounds.Contains(mousePos);




                if (collided)
                {
                    Debug.Log("Touched collider " );
                }
                else
                {
                    Debug.Log("Not collided");
                }
    }
if (eventData.pointerEnter.gameObject.name == "redcircle2(Clone)")

我很确定这一行就是错误的来源。 如果你正在实例化,那么我会猜测你是从一个 public 转换实例化,然后你已经放置了一些已经添加了碰撞的预制件,对吧? 然后检查你添加的 PREFAB 有什么游戏标签并检测到,而不是 "redcircle2(Clone)".

Unity 中有 2 个完全不同且独立的物理系统 - Physics(3D) 和 Physics2D

如前所述,它们是分开的,不会以任何方式相互作用,例如Collider 永远不会跟踪与 Collider2D.

的任何碰撞

同样适用于PhysicsRaycasterPhysicsRaycaster2D!

在你的场景中你有一个 CapsuleCollider 这是一个 3D 对撞机。

它只能通过使用 PhysicsRaycaster - 而不是 通过 PhysicsRaycaster2D!

进行跟踪

→要么你的 Camera 应该有一个 PhysicsRaycaster 而不是 PhysicsRaycaster 2D 或者如果它应该是一个 2D 游戏无论如何你应该只使用 2D 碰撞器。