/* * Provides raycast data based on where a SphereCast would have contacted * the specified normal. * Raycasting downwards from a point along the controller's bottom sphere, * based on the provided normal. */ private bool SimulateSphereCast(CollisionSphere collisionSphere, Vector3 groundNormal, out RaycastHit hit) { float groundAngle = Vector3.Angle(groundNormal, playerView.Up) * Mathf.Deg2Rad; Vector3 secondaryOrigin = playerView.Position + (playerView.Up * settings.tolerance); if (!Mathf.Approximately(groundAngle, 0)) { float horizontal = Mathf.Sin(groundAngle) * collisionSphere.Radius; float vertical = (1f - Mathf.Cos(groundAngle)) * collisionSphere.Radius; Vector3 upslopeDirection = -CollisionMath.DownslopeDirection(groundNormal, playerView.Down); Vector3 horizontalDirection = Math3d.ProjectVectorOnPlane(playerView.Up, upslopeDirection).normalized; secondaryOrigin += horizontalDirection * horizontal + playerView.Up * vertical; } if (SphereCast(secondaryOrigin, settings.epsilon, playerView.Down, out hit)) { hit.distance -= settings.tolerance; return(true); } return(false); }
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); } } }
private void SlopeLimit(Vector3 initialPosition) { Vector3 groundNormal = grounding.PrimaryNormal; float groundAngle = Vector3.Angle(groundNormal, playerView.Up); if (groundAngle > grounding.Collidable.SlopeLimit) { Vector3 absoluteMoveDirection = Math3d.ProjectVectorOnPlane( groundNormal, playerView.Position - initialPosition ); Vector3 downslopeDirection = CollisionMath.DownslopeDirection(groundNormal, playerView.Down); float slopeAngle = Vector3.Angle(absoluteMoveDirection, downslopeDirection); if (slopeAngle <= settings.slopeLimitUpperBoundAngle) { return; } Vector3 resolvedPosition = Math3d.ProjectPointOnLine( initialPosition, Vector3.Cross(groundNormal, playerView.Down), playerView.Position ); Vector3 direction = Math3d.ProjectVectorOnPlane( groundNormal, resolvedPosition - playerView.Position ); RaycastHit hit; if (Physics.CapsuleCast( feetSphere.Position, headSphere.Position, maxCollisionSphereRadius, direction.normalized, out hit, direction.magnitude, settings.walkableLayerMask )) { playerView.Position += downslopeDirection * hit.distance; } else { playerView.Position += direction; } } }
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"); } } }