void HorizontalCollisions(ref Vector2 moveAmount) { float directionX = collisions.faceDir; float rayLength = Mathf.Abs(moveAmount.x) + skinWidth; //create a layerMask from collisionMask, but exclude OneWayPlatforms on the sides. LayerMask adjustedMaskHorizontal = LayerMaskExtensions.RemoveFromMask(collisionMask, oneWayPlatformLayer); if (Mathf.Abs(moveAmount.x) < skinWidth) { // rayLength = 2*skinWidth; } for (int i = 0; i < horizontalRayCount; i++) { //disables the bottom horizontal collision check when moving upwards //This was causing the player to 'hit' the slope when jumping, cancelling its velocity //if (i == 0 && rawMoveAmount.y > 0) continue; Vector2 rayOrigin = (directionX == -1)?raycastOrigins.bottomLeft:raycastOrigins.bottomRight; rayOrigin += Vector2.up * (horizontalRaySpacing * i); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, adjustedMaskHorizontal); //Debug.DrawRay(rayOrigin, Vector2.right * directionX / 2, Color.white); Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, Color.red); if (hit) { if (hit.distance == 0) { continue; } float forwardSlopeAngle = Vector2.Angle(hit.normal, Vector2.up); //collisions.slopeAngle = forwardSlopeAngle; //forward hit from foot has detected slope ahead if (i == 0 && forwardSlopeAngle <= maxSlopeAngle) { if (collisions.descendingSlope) { Debug.Log("Case 1"); collisions.descendingSlope = false; moveAmount = collisions.moveAmountOld; } float distanceToSlopeStart = 0; //slope has changed! if (forwardSlopeAngle != collisions.slopeAngleOld) { Debug.Log("Slope ahead of player changed from " + collisions.slopeAngleOld + " to " + forwardSlopeAngle); distanceToSlopeStart = hit.distance - skinWidth; moveAmount.x -= distanceToSlopeStart * directionX; } ClimbSlope(ref moveAmount, forwardSlopeAngle, hit.normal); moveAmount.x += distanceToSlopeStart * directionX; } if (!collisions.climbingSlope || forwardSlopeAngle > maxSlopeAngle) { moveAmount.x = (hit.distance - skinWidth) * directionX; rayLength = hit.distance; if (collisions.climbingSlope) { moveAmount.y = Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(moveAmount.x); } collisions.left = directionX == -1; collisions.right = directionX == 1; } } } }
void VerticalCollisions(ref Vector2 moveAmount) { float directionY = Mathf.Sign(moveAmount.y); float rayLength = Mathf.Abs(moveAmount.y) + skinWidth; //if falling through platforms, remove oneWayPlatform layer from the mask. LayerMask adjustedMaskVertical = collisionMask; if (collisions.fallingThroughPlatform) { adjustedMaskVertical = LayerMaskExtensions.RemoveFromMask(adjustedMaskVertical, oneWayPlatformLayer); Debug.Log(adjustedMaskVertical.ToString()); } for (int i = 0; i < verticalRayCount; i++) { Vector2 rayOrigin = (directionY == -1)?raycastOrigins.bottomLeft:raycastOrigins.topLeft; rayOrigin += Vector2.right * (verticalRaySpacing * i + moveAmount.x); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, adjustedMaskVertical); //Debug.DrawRay(rayOrigin, Vector2.up * directionY / 2, Color.white); Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red); if (hit) { //if the Layer matches the name of the OneWayPlatformLayer, then ignore it when going up. if (LayerMask.LayerToName(hit.collider.gameObject.layer) == oneWayPlatformLayer) { if (directionY == 1 || hit.distance == 0) { continue; } //BUG: //disregard hits if falling through platform. //if (collisions.fallingThroughPlatform) { //continue; //} //if the player inputs down, will pass through oneway platforms briefly. if (m_playerInput.y == -1) { collisions.fallingThroughPlatform = true; Invoke("ResetFallingThroughPlatform", .5f); continue; } } //main collision property moveAmount.y = (hit.distance - skinWidth) * directionY; rayLength = hit.distance; if (collisions.climbingSlope) { Debug.Log("Vertical Collisions Climbing Slope moveAmountX mod"); moveAmount.x = moveAmount.y / Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Sign(moveAmount.x); } collisions.below = directionY == -1; collisions.above = directionY == 1; } } }