Unity2d中射线检测物体

   void Update()
    {
     

        if(Input.GetMouseButton(0))
        {
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition),Vector2.zero);
            if(hit.collider!=null)
            {
                //HitObj是你需要检测的物体的标签
                if(hit.collider.tag=="HitObj")
                {
                    Destroy(hit.transform.parent.gameObject);
                }
            }
        }

    }

 

你可能感兴趣的:(Unity,Unity学习)