コード例 #1
0
 private void ResetGrounds()
 {
     primaryGround = new GroundHit();
     nearGround    = new GroundHit();
     farGround     = new GroundHit();
     flushGround   = new GroundHit();
     stepGround    = new GroundHit();
 }
コード例 #2
0
        private void HandleEdgeCollision(CollisionSphere collisionSphere, RaycastHit hit)
        {
            Vector3 towardCenter = Math3d.ProjectVectorOnPlane(
                playerView.Up,
                (playerView.Position - hit.point).normalized * settings.epsilon
                );

            Quaternion planeAwayRotation = Quaternion.AngleAxis(
                settings.edgeCollisionRotateDegree,
                Vector3.Cross(towardCenter, playerView.Up)
                );

            Vector3 awayCenter = planeAwayRotation * -towardCenter;

            Vector3 nearPoint = hit.point + towardCenter + (playerView.Up * settings.epsilon);
            Vector3 farPoint  = hit.point + (awayCenter * settings.edgeCollisionFarPointMultiplier);

            RaycastHit nearHit;
            RaycastHit farHit;

            Raycast(nearPoint, playerView.Down, out nearHit);
            Raycast(farPoint, playerView.Down, out farHit);

            nearGround = new GroundHit(nearHit);
            farGround  = new GroundHit(farHit);

            // If we are standing on surface that should be counted as a
            // wall, attempt to flush against it on the ground
            if (Vector3.Angle(hit.normal, playerView.Up) > collidable.StandAngle)
            {
                FlushGround(collisionSphere, hit);
            }

            // If we are standing on a ledge then face the nearest center of
            // the player view, which should be steep enough to be counted as
            // a wall. Retrieve the ground it is connected to at its base, if
            // there is one.
            if ((Vector3.Angle(nearHit.normal, playerView.Up) > collidable.StandAngle) || (nearHit.distance > settings.tolerance))
            {
                Collidable nearCollidable = GetCollidable(nearHit);

                if (Vector3.Angle(nearHit.normal, playerView.Up) > nearCollidable.StandAngle)
                {
                    Vector3 downslopeDirection = CollisionMath.DownslopeDirection(nearHit.normal, playerView.Down);

                    RaycastHit stepHit;

                    if (Raycast(nearPoint, downslopeDirection, out stepHit))
                    {
                        stepGround = new GroundHit(stepHit);
                    }
                }
                else
                {
                    stepGround = new GroundHit(nearHit);
                }
            }
        }
コード例 #3
0
 private void DebugGround(GroundHit groundHit, Color color)
 {
     if (groundHit.isFound)
     {
         DebugDraw.DrawVector(
             groundHit.point,
             groundHit.normal,
             2f,
             1f,
             color,
             0,
             false
             );
     }
 }
コード例 #4
0
        private void HandleClippingCollision(CollisionSphere collisionSphere, RaycastHit hit)
        {
            collidable = GetCollidable(hit);
            transform  = hit.transform;

            RaycastHit sphereCastHit;

            if (SimulateSphereCast(collisionSphere, hit.normal, out sphereCastHit))
            {
                primaryGround = new GroundHit(sphereCastHit);
            }
            else
            {
                primaryGround = new GroundHit(hit);
            }
        }
コード例 #5
0
        /*
         * Scans surface below controller for ground. Follows up the initial
         * scan with subsequent scans to handle different edge cases.
         */
        public void ProbeGround(CollisionSphere collisionSphere)
        {
            ResetGrounds();

            Vector3 origin = collisionSphere.Position + (playerView.Up * settings.tolerance);

            // Reduce radius to avoid failing SphereCast due to wall clipping
            float smallerRadius = collisionSphere.Radius - (settings.tolerance * settings.tolerance);

            RaycastHit hit;

            if (SphereCast(origin, smallerRadius, playerView.Down, out hit))
            {
                collidable = GetCollidable(hit);
                transform  = hit.transform;

                SimulateSphereCast(collisionSphere, hit.normal, out hit);

                primaryGround = new GroundHit(hit);

                // If we are standing on a perfectly flat surface, we cannot be
                // either on an edge, on a slope, or stepping off a ledge
                Vector3 surface = Math3d.ProjectPointOnPlane(playerView.Up, playerView.Position, hit.point);

                if (Vector3.Distance(surface, playerView.Position) < settings.epsilon)
                {
                    return;
                }

                // Now the collision is on an edge, so normals of the two faces
                // on either side of the edge are computed and stored in nearHit
                // and farHit
                HandleEdgeCollision(collisionSphere, hit);
            }
            // If initial SphereCast fails, which is likely due to controller
            // clipping a wall, fallback to raycast simulating SphereCast data
            else if (Raycast(origin, playerView.Down, out hit))
            {
                HandleClippingCollision(collisionSphere, hit);
            }
            else
            {
                Debug.Log("No ground found below player");
            }
        }
コード例 #6
0
        private void FlushGround(CollisionSphere collisionSphere, RaycastHit hit)
        {
            Vector3 downslopeDirection = CollisionMath.DownslopeDirection(hit.normal, playerView.Down);

            Vector3 flushOrigin = hit.point + (hit.normal * settings.epsilon);

            RaycastHit flushHit;

            if (Raycast(flushOrigin, downslopeDirection, out flushHit))
            {
                RaycastHit sphereCastHit;

                if (SimulateSphereCast(collisionSphere, flushHit.normal, out sphereCastHit))
                {
                    flushGround = new GroundHit(sphereCastHit);
                }
                else
                {
                    Debug.Log("Failed to flush against ground");
                }
            }
        }
コード例 #7
0
 private bool IsGroundStandable(GroundHit groundHit)
 {
     return(Vector3.Angle(groundHit.normal, playerView.Up) < collidable.StandAngle);
 }