Example #1
0
        /// <summary>
        /// Preform a spherecast, and return the result
        /// </summary>
        /// <param name="correctedPosition"></param>
        /// <param name="surfaceNormal"></param>
        /// <param name="grounded"></param>
        /// <returns></returns>
        private bool GetGroundCapsule(out Vector3 correctedPosition, out Vector3 surfaceNormal, bool grounded)
        {
            //Set up raycast to preform a sphere cast
            raycast.sphereCastRadius = radius;
            //Set the distance of the cast based on grounded
            SetDistance(grounded);

            //Get a set of hits for the raycast
            RaycastHit[] hits = raycast.CastAll();
            foreach (var hit in hits)
            {
                //Debug.Log(hit.ToString());
                //Debug.Log(hit.point.ToString());
                //Debug.Log(hit.normal.ToString());
                //Validate each hit

                //Determine the distance to the hit (on the verticle axis only)
                float dot = Vector3.Dot(direction, hit.point - raycast.origin);
                //If the hit is extended past the expected distance, skip this hit
                if (dot > raycast.distance)
                {
                    continue;
                }

                //Correct the point to be under the characters feet
                Vector3 groundPoint;
                Math3d.LinePlaneIntersection(out groundPoint, raycast.origin, direction, direction, hit.point);

                //Determine the corrected position, and surface normal
                //Note: Even though a sphere cast is preformed, the results are treated more like a cylender cast
                correctedPosition = groundPoint + -direction * hoverDistance;
                surfaceNormal     = hit.normal;

                //Determine if the character is floating, an set the surface normal directly up
                if ((groundPoint != hit.point))
                {
                    surfaceNormal = -direction;
                }

                return(true);
            }

            //Initialize default output
            correctedPosition = surfaceNormal = Vector3.zero;
            return(false);
        }