public bool CheckLedgeAcross(DirectionFacing direction, Bounds?projectedBounds = null) { NextClimbs.Clear(); Bounds bounds = projectedBounds != null ? (Bounds)projectedBounds : _motor.Collider.bounds; float maxNonHangDistance = 6f; float checkLength = 7f; const float maxHeightAbove = 1f; const float maxHeightBelow = 2f; const float spaceInFront = 2f; float checkDepth = bounds.size.y + maxHeightAbove + maxHeightBelow; float x; if (CurrentClimbingState.PivotCollider == null || CurrentClimbingState.CanClimbDown) { x = direction == DirectionFacing.Left ? bounds.min.x - spaceInFront : bounds.max.x + spaceInFront; checkLength -= spaceInFront; } else { x = direction == DirectionFacing.Left ? bounds.min.x : bounds.max.x; } float y = bounds.center.y - maxHeightBelow + maxHeightAbove; var origin = new Vector2(x, y); var size = new Vector2(0.01f, checkDepth); Vector2 castDirection = direction == DirectionFacing.Left ? Vector2.left : Vector2.right; RaycastHit2D[] hits = Physics2D.BoxCastAll(origin, size, 0, castDirection, checkLength, GetClimbMask()); hits = RemoveInvalidColliders(hits); RaycastHit2D hit = hits.FirstOrDefault(h => h.point.y < bounds.center.y && Mathf.Abs(h.point.y - bounds.center.y) < maxNonHangDistance && EdgeValidator.CanJumpToOrFromEdge(h.collider, bounds.center, _motor.CrouchedCollider)); bool shouldHang = false; Climb directionalAcross = direction == DirectionFacing.Left ? Climb.AcrossLeft : Climb.AcrossRight; if (hit) { shouldHang = CurrentClimbingState.PivotCollider != null && hit.collider.transform.parent == CurrentClimbingState.PivotCollider.transform.parent; if (shouldHang && EdgeValidator.CanJumpToHang(hit.collider, bounds.center, _motor.StandingCollider) == false) { hit = new RaycastHit2D(); } } else { hit = hits.FirstOrDefault(h => EdgeValidator.CanJumpToHang(h.collider, bounds.center, _motor.StandingCollider)); if (hit) { shouldHang = true; } } if (hit) { NextClimbingState = GetClimbingState(directionalAcross, hit.collider, shouldHang); _anim.SetBool("shouldHang", shouldHang); _anim.SetBool("inverted", ClimbSide == direction); } return(hit); }
public bool CheckLedgeBelow(Climb intendedClimbingState, DirectionFacing direction, out string animation) { //check the edge in the direction travelling. If it's near enough, move to it and jump unless it's dropless. //else, if the slope is declining, set the current spot as the edge and jump, unless there's a dropless edge and we're only jumping bool nearEdge = false; bool canDrop = true; bool down = intendedClimbingState == Climb.Down; if (intendedClimbingState == Climb.Jump) { intendedClimbingState = direction == DirectionFacing.Left ? Climb.AcrossLeft : Climb.AcrossRight; } if (down == false && _motor.MovementState.PivotCollider != null && _motor.MovementState.PivotCollider.gameObject.layer == LayerMask.NameToLayer(Layers.Ice)) { if (JumpInPlace(direction, canDrop, out animation)) { return(true); } } const float checkWidth = 4f; int layer = direction == DirectionFacing.Right ? _rightClimbLayer : _leftClimbLayer; BoxCollider2D edge = null; try { edge = _motor.MovementState.PivotCollider.gameObject.GetComponentsInChildren <BoxCollider2D>().SingleOrDefault(c => c.gameObject.layer == layer); if (edge == null) { var climbableEdges = _motor.MovementState.PivotCollider.gameObject.GetComponent <ClimbableEdges>(); if (climbableEdges != null) { Orientation orientation = OrientationHelper.GetOrientation(_motor.MovementState.PivotCollider.transform); Collider2D excluded = null; if (orientation == Orientation.Flat) { if (direction == DirectionFacing.Left && climbableEdges.IsLeftCorner && climbableEdges.IsLeftCornerInverted == false) { excluded = climbableEdges.LeftException; } else if (direction == DirectionFacing.Right && climbableEdges.IsRightCorner && climbableEdges.IsRightCornerInverted == false) { excluded = climbableEdges.RightException; } } else if (orientation == Orientation.UpsideDown) { if (direction == DirectionFacing.Left && climbableEdges.IsRightCorner && climbableEdges.IsRightCornerInverted) { excluded = climbableEdges.RightException; } else if (direction == DirectionFacing.Right && climbableEdges.IsLeftCorner && climbableEdges.IsLeftCornerInverted) { excluded = climbableEdges.LeftException; } } if (excluded != null) { edge = excluded.gameObject.GetComponentsInChildren <BoxCollider2D>().SingleOrDefault(c => c.gameObject.layer == layer); } } } } catch (Exception) { } Bounds bounds = _motor.Collider.bounds; bool facingEdge = (_motor.MovementState.LeftEdge && direction == DirectionFacing.Left) || (_motor.MovementState.RightEdge && direction == DirectionFacing.Right); if (edge != null && (facingEdge || EdgeValidator.CanJumpToOrFromEdge(edge, bounds.center, _motor.CrouchedCollider))) { canDrop = edge.CanClimbDown(); var distance = Vector2.Distance(_motor.GetGroundPivotPosition(), edge.transform.position); nearEdge = distance < checkWidth; Bounds projectedBounds = new Bounds( new Vector3( direction == DirectionFacing.Right ? edge.transform.position.x + bounds.extents.x : edge.transform.position.x - bounds.extents.x, edge.transform.position.y + bounds.extents.y), bounds.size); if (nearEdge && ((down && EdgeValidator.CanClimbUpOrDown(edge, _motor.CrouchedCollider)) || (down == false && (CheckLedgeAcross(direction, projectedBounds) || canDrop)))) { CurrentClimbingState = GetClimbingState(intendedClimbingState, edge); if (down) { animation = EdgeValidator.CanHang(edge, _motor.StandingCollider) == false ? Animations.HopDown : _motor.Anim.GetBool(PlayerAnimBool.Moving) ? Animations.RollDown : Animations.ClimbDown; } else { animation = Animations.DiveAcross; } return(true); } } if (edge == null && down && facingEdge) { CurrentClimbingState = GetStaticClimbingState(); animation = Animations.HopDown; _exception = null; return(true); } bool downhill = _motor.MovementState.NormalDirection == direction && Vector2.Angle(Vector2.up, _motor.MovementState.Normal) > 20f; if (downhill && down == false) { _exception = edge; if (JumpInPlace(direction, canDrop, out animation)) { return(true); } } animation = ""; return(false); }