예제 #1
0
    private void Update()
    {
        var shoulderPosition = hand.isLeft
            ? head.TransformPoint(new Vector3(-shoulderWidth, -.13f, 0f))
            : head.TransformPoint(new Vector3(shoulderWidth, -.13f, 0f));

        direction = (hand.stablePinchPos() - shoulderPosition).normalized;

        var ray = new Ray(shoulderPosition, direction);

        RaycastHit hit;

        WatchAirtap();

        if (hand.isTracked && Physics.Raycast(ray, out hit, 10, layermask))
        {
            targetChanged = hovering && hit.collider.transform != hoveredTransform && !pinching;

            if ((!hovering || targetChanged) && hit.collider != null)
            {
                if (targetChanged)
                {
                    currentTarget.SetHit(whichHand, Vector3.zero, false);
                    if (currentTarget.OnHoverEnd != null && !currentTarget.isHovered)
                    {
                        currentTarget.OnHoverEnd();
                    }
                }

                currentTarget = hit.collider.gameObject.GetComponent <HandRaycastTarget>();
                currentTarget.hoveringRaycast = this;

                if (currentTarget != null)
                {
                    if (!currentTarget.isHovered)
                    {
                        currentTarget.SetHit(whichHand, hit.point, true);
                        if (currentTarget.OnHoverBegin != null && !currentTarget.isHovered)
                        {
                            currentTarget.OnHoverBegin();
                        }
                    }

                    hoveredTransform = hit.collider.transform;
                }

                if (OnHoverBegin != null)
                {
                    OnHoverBegin();
                }
                hovering = true;
            }

            if (hovering && currentTarget != null)
            {
                currentTarget.SetHit(whichHand, hit.point, true);
            }

            if (!pinching && pg.isActive)
            {
                pinching = true;
                if (OnPinchBegin != null)
                {
                    OnPinchBegin();
                }
                if (currentTarget.OnPinchBegin != null)
                {
                    currentTarget.OnPinchBegin();
                }

                lastPinchTravel  = 0;
                prevPinchPos     = hand.pinchPos();
                onDragPinchPos   = hand.pinchPos();
                currentDragDelta = Vector3.zero;
            }

            if (tapping)
            {
                if (OnAirTap != null)
                {
                    OnAirTap();
                }
                if (currentTarget.OnAirTap != null)
                {
                    currentTarget.OnAirTap();
                }
            }
        }
        else
        {
            if (hovering && !pinching)
            {
                if (OnHoverEnd != null)
                {
                    OnHoverEnd();
                }
                if (currentTarget != null)
                {
                    currentTarget.SetHit(whichHand, Vector3.zero, false);
                    if (currentTarget.OnHoverEnd != null && !currentTarget.isHovered)
                    {
                        currentTarget.OnHoverEnd();
                    }
                }

                hoveredTransform = null;
                hovering         = false;
            }
        }


        if (pinching)
        {
            lastPinchTravel += hand.pinchPos().sqrDist(prevPinchPos);
            currentDragDelta = hand.pinchPos() - onDragPinchPos;
        }

        if (pinching && !pg.isActive)
        {
            pinching = false;
            if (OnPinchEnd != null)
            {
                OnPinchEnd();
            }
            if (currentTarget.OnPinchEnd != null)
            {
                currentTarget.OnPinchEnd();
            }

            if (lastPinchTravel < travelThreshold)
            {
                if (currentTarget.OnPinchClick != null)
                {
                    currentTarget.OnPinchClick();
                }
                if (OnPinchClick != null)
                {
                    OnPinchClick();
                }
//                Debug.Log("click! travel: " + lastPinchTravel);
            }
        }

        tapping      = false;
        prevPinchPos = hand.pinchPos();
    }