bool CheckSlopeAngle(SlopeCheck slopeCheck) { switch (slopeCheck) { case SlopeCheck.Front: if (slopeAngleFront <= playerController.playerControllerData.maximumSlopeSlide && slopeAngleFront > 0) { touchingSlopeFront = true; CheckSlopeIsSteep(slopeAngleFront); return(true); } touchingSlopeFront = false; break; case SlopeCheck.Back: if (slopeAngleBack <= playerController.playerControllerData.maximumSlopeSlide && slopeAngleBack > 0) { touchingSlopeBack = true; CheckSlopeIsSteep(slopeAngleBack); return(true); } touchingSlopeBack = false; break; default: case SlopeCheck.None: break; } return(false); }
private bool TouchingTerrain(Vector2 direction, CheckType checkType, int numberOfRaycasts, float length, SlopeCheck slopeCheck) { #if UNITY_EDITOR // Only used with debug setup for displaying hit raycasts bool hit = false; #endif switch (checkType) { case CheckType.Vertical: offset = new Vector2(spriteRenderer.size.x, 0); Physics2D.queriesHitTriggers = !playerController.playerControllerData.raycastVerticalIgnoreTriggers; break; case CheckType.Horizontal: offset = new Vector2(0, spriteRenderer.size.y); Physics2D.queriesHitTriggers = !playerController.playerControllerData.raycastHorizontalIgnoreTriggers; break; } Vector2 minimumBounds = -offset / 2.21f; Vector2 maximumBounds = offset / 2.21f; switch (checkType) { case CheckType.Vertical: minimumBounds.x -= playerController.playerControllerData.raycastSpreadAmountVertical; maximumBounds.x += playerController.playerControllerData.raycastSpreadAmountVertical; break; case CheckType.Horizontal: minimumBounds.y -= playerController.playerControllerData.raycastSpreadAmountHorizontal; maximumBounds.y += playerController.playerControllerData.raycastSpreadAmountHorizontal; break; } for (int i = 0; i < numberOfRaycasts; i++) { Vector2 pos = new Vector2(); if (numberOfRaycasts > 1) { pos = Vector2.Lerp(minimumBounds, maximumBounds, (float)i / (float)(numberOfRaycasts - 1)); } else { switch (slopeCheck) { case SlopeCheck.Front: if (myTransform.localScale.x > 0) { pos = (maximumBounds); } else { pos = (minimumBounds); } break; case SlopeCheck.Back: if (myTransform.localScale.x > 0) { pos = (minimumBounds); } else { pos = (maximumBounds); } break; } } var hitRay = Physics2D.Raycast(pos + (Vector2)transform.position, direction, length, playerController.playerControllerData.terrainLayer); if (hitRay) { switch (slopeCheck) { case SlopeCheck.Front: Debug.DrawRay(pos + (Vector2)transform.position, direction * length, Color.green); slopeAngleFront = Vector2.Angle(hitRay.normal, Vector2.up); hitPointFront = hitRay.point; #if UNITY_EDITOR hit = CheckSlopeAngle(slopeCheck); return(hit); #endif return(true); case SlopeCheck.Back: Debug.DrawRay(pos + (Vector2)transform.position, direction * length, Color.green); slopeAngleBack = Vector2.Angle(hitRay.normal, Vector2.up); hitPointBack = hitRay.point; #if UNITY_EDITOR hit = CheckSlopeAngle(slopeCheck); return(hit); #endif return(true); default: case SlopeCheck.None: break; } if (checkType == CheckType.Horizontal && slopeCheck == SlopeCheck.None) { touchWallAngle = Vector2.Angle(hitRay.normal, Vector2.up); } #if UNITY_EDITOR if (i == 0) { Debug.DrawRay(pos + (Vector2)transform.position, direction * length, Color.green); hit = true; continue; } Debug.DrawRay(pos + (Vector2)transform.position, direction * length, Color.red); hit = true; continue; #endif return(true); } #if UNITY_EDITOR Debug.DrawRay(pos + (Vector2)transform.position, direction * length, Color.blue); #endif switch (slopeCheck) { case SlopeCheck.Front: slopeAngleFront = 0; hitPointFront = pos + (Vector2)transform.position + direction * length; #if UNITY_EDITOR hit = false; return(hit); #endif return(false); case SlopeCheck.Back: slopeAngleBack = 0; hitPointBack = pos + (Vector2)transform.position + direction * length; #if UNITY_EDITOR hit = false; return(hit); #endif return(false); default: case SlopeCheck.None: break; } } #if UNITY_EDITOR return(hit); #endif return(false); } // End Touch Wall