Ejemplo n.º 1
0
        // Skip check for near/far plane!
        private bool TryGetHitDistance(Ray ray, ref float hitDistance, out RaycastHit hitResult)
        {
            float maxDistance = hitDistance;
            int   allraycasts = -1;

            allraycasts = Physics.RaycastNonAlloc(ray, st_arrayOfRaycasts, maxDistance, m_BlockingMask);
            if (allraycasts > 0)
            {
                if (allraycasts > 1)
                {
                    System.Array.Sort(st_arrayOfRaycasts, 0, allraycasts, VRTK3DRaycastHitComparer.instance);
                }

                RaycastHit hit;
                for (int i = 0; i < allraycasts && i < st_arrayOfRaycasts.Length; i++)
                {
                    hit = st_arrayOfRaycasts[i];
                    if (hit.collider != null && !VRTK4_PlayerObject.IsPlayerObject(hit.collider.gameObject))
                    {
                        hitDistance = hit.distance;
                        hitResult   = hit;
                        return(true);
                    }
                }
            }

            hitResult = default;
            return(false);
        }
Ejemplo n.º 2
0
        // Skip check for near/far plane!
        protected virtual float GetHitDistance(Ray ray, float hitDistance)
        {
            if (CanvasToUse.renderMode != RenderMode.ScreenSpaceOverlay && blockingObjects != BlockingObjects.None)
            {
                float maxDistance = Vector3.Distance(ray.origin, CanvasToUse.transform.position) + 10f;
                int   allraycasts = -1;
                if (blockingObjects == BlockingObjects.ThreeD || blockingObjects == BlockingObjects.All)
                {
                    allraycasts = Physics.RaycastNonAlloc(ray, st_arrayOfRaycasts, maxDistance, m_BlockingMask);
                    if (allraycasts > 0)
                    {
                        RaycastHit hit = st_arrayOfRaycasts[0];
                        if (hit.collider != null && !VRTK4_PlayerObject.IsPlayerObject(hit.collider.gameObject))
                        {
                            hitDistance = hit.distance;
                        }
                    }

                    ClearArrNonAlloc3D();
                }

                if (blockingObjects == BlockingObjects.TwoD || blockingObjects == BlockingObjects.All)
                {
                    allraycasts = Physics2D.RaycastNonAlloc(ray.origin, ray.direction, st_arrayOfRaycasts2d,
                                                            maxDistance,
                                                            m_BlockingMask);
                    if (allraycasts > 0)
                    {
                        RaycastHit2D hit = st_arrayOfRaycasts2d[0];
                        if (hit.collider != null && !VRTK4_PlayerObject.IsPlayerObject(hit.collider.gameObject))
                        {
                            hitDistance = hit.fraction * maxDistance;
                        }
                    }

                    ClearArrNonAlloc2D();
                }
            }

            return(hitDistance);
        }