// Static version: This is where the calculation is done
        // Raycast that
        static public RaycastHit2D?DoMouseRaycast2D(Raycast2DManager managerInstance, Camera pRaycastCamera, string[] tags)
        {
            //GameObject.Find("t1").GetComponent<UnityEngine.UI.Text>().text = Display.RelativeMouseAt(Input.mousePosition).x.ToString();

            Ray ray = pRaycastCamera.ScreenPointToRay(Input.mousePosition);

            RaycastHit2D[] hits = Physics2D.RaycastAll(ray.origin, ray.direction);
            // Return first raycast
            foreach (RaycastHit2D hit in hits)
            {
                //Debug.DrawLine(pRaycastCamera.transform.position - new Vector3(0,0,10), hit.transform.position, Color.red);

                bool lHasOneTag = false;
                foreach (string tag in tags)
                {
                    if (hit.transform.tag == tag)
                    {
                        lHasOneTag = true;
                    }
                }
                if (lHasOneTag)
                {
                    managerInstance.OnRaycast2DMouseHit.Invoke(hit);
                    return(hit);
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        // Called when this component is attached in editor
        private void Initialize()
        {
            raycast2DManager = GetComponent <Raycast2DManager>();
            // Start drag
            raycast2DManager.OnRaycast2DMouseDown_left.AddListener((RaycastHit2D pHit) => {
                if (pHit.transform == null || !interactable)
                {
                    return;
                }



                // pHit.transform exists, so we start dragging it
                if (raycast2DManager.Logs)
                {
                    Debug.Log("Started dragging: " + pHit.transform.name);
                }
                mDragged            = pHit.transform;
                mIndexAsChildBuffer = mDragged.GetSiblingIndex();
                mDragged.transform.SetAsLastSibling();
                mDraggedInitialPos = mDragged.transform.position;
                mMouseInitialPos   = raycast2DManager.raycastCamera.ScreenToWorldPoint(Input.mousePosition);
                OnDragStart.Invoke(mDragged);
            });
        }