/// <summary>
        /// Checks whether the point is inside a collider.
        /// </summary>
        /// <remarks>
        /// This method can check against all types of colliders,
        /// including <c>TerrainCollider</c> and concave <c>MeshCollider</c>.
        /// </remarks>
        /// <param name="collider">
        /// The collider to check against.
        /// </param>
        /// <param name="point">
        /// The point being checked.
        /// </param>
        /// <param name="specificTest">
        /// Defines a kind of specific collision test that must be done against <paramref name="collider"/>.
        /// </param>
        /// <returns>
        /// <c>true</c> if the <paramref name="point"/> is inside the <paramref name="collider"/>,
        /// <c>false</c> otherwise.
        /// </returns>
        public static bool IsPointInsideCollider(Collider collider, Vector3 point, ColliderSpecificTestEnum specificTest = ColliderSpecificTestEnum.None)
        {
            RaycastHit hit;

            if (specificTest == ColliderSpecificTestEnum.None)
            {
#if !UNITY_FLASH
                if (collider is TerrainCollider)
                {
                    if (!collider.Raycast(new Ray(point, Vector3.up), out hit, collider.bounds.size.y))
                    {
                        return(false);
                    }
                }
                else
#endif
                if (collider is MeshCollider && !((MeshCollider)collider).convex)
                {
                    if (!IsPointInsideMeshCollider(collider, point))
                    {
                        return(false);
                    }
                }
                else
                {
                    Vector3 direction          = collider.bounds.center - point;
                    float   directionMagnitude = direction.sqrMagnitude;
                    if (directionMagnitude > 0.0001f &&
                        collider.Raycast(new Ray(point, direction.normalized), out hit, FastFunctions.FastSqrt(directionMagnitude)))
                    {
                        return(false);
                    }
                }
            }
            else
            {
                if (specificTest == ColliderSpecificTestEnum.Terrain)
                {
                    if (!collider.Raycast(new Ray(point + Vector3.up * collider.bounds.size.y, Vector3.down), out hit, collider.bounds.size.y))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks whether the point is inside a collider.
        /// </summary>
        /// <remarks>
        /// This method can check against all types of colliders, 
        /// including <c>TerrainCollider</c> and concave <c>MeshCollider</c>.
        /// </remarks>
        /// <param name="collider">
        /// The collider to check against.
        /// </param>
        /// <param name="point">
        /// The point being checked.
        /// </param>
        /// <param name="specificTest">
        /// Defines a kind of specific collision test that must be done against <paramref name="collider"/>.
        /// </param>
        /// <returns>
        /// <c>true</c> if the <paramref name="point"/> is inside the <paramref name="collider"/>, 
        /// <c>false</c> otherwise.
        /// </returns>
        public static bool IsPointInsideCollider(Collider collider, Vector3 point, ColliderSpecificTestEnum specificTest = ColliderSpecificTestEnum.None) {
            RaycastHit hit;
            if (specificTest == ColliderSpecificTestEnum.None) {
#if !UNITY_FLASH
                if (collider is TerrainCollider) {
                    if (!collider.Raycast(new Ray(point, Vector3.up), out hit, collider.bounds.size.y)) {
                        return false;
                    }
                } else
#endif
                    if (collider is MeshCollider && !((MeshCollider) collider).convex) {
                        if (!IsPointInsideMeshCollider(collider, point)) {
                            return false;
                        }
                    } else {
                        Vector3 direction = collider.bounds.center - point;
                        float directionMagnitude = direction.sqrMagnitude;
                        if (directionMagnitude > 0.0001f &&
                            collider.Raycast(new Ray(point, direction.normalized), out hit, FastFunctions.FastSqrt(directionMagnitude))) {
                            return false;
                        }
                    }
            } else {
                if (specificTest == ColliderSpecificTestEnum.Terrain) {
                    if (!collider.Raycast(new Ray(point + Vector3.up * collider.bounds.size.y, Vector3.down), out hit, collider.bounds.size.y)) {
                        return false;
                    }
                }
            }

            return true;
        }