void VerticalCollisions(ref Vector2 moveAmount) { float directionY = Mathf.Sign(moveAmount.y); float rayLength = Mathf.Abs(moveAmount.y) + skinWidth; 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, collisionMask); Debug.DrawRay(rayOrigin, Vector2.up * directionY, Color.red); if (hit) { if (hit.collider.CompareTag("Coin")) { CoinController coinController = hit.collider.GetComponent <CoinController>(); coinController.GetCoin(); Destroy(hit.collider.gameObject); } if (hit.collider.CompareTag("Enemy")) { if (directionY == -1) { Destroy(hit.collider.gameObject); enemyController.AttackEnemy(); } else if (!enemyController.isGameOver) { Debug.Log("GameOver"); enemyController.gameOver(); } } if (hit.collider.CompareTag("Through")) { if (directionY == 1 || hit.distance == 0) { continue; } if (collisions.fallingThroughPlatform) { continue; } if (playerInput.y == -1) { collisions.fallingThroughPlatform = true; Invoke("ResetFallingThroughPlatform", 0.5f); continue; } } if (hit.collider.CompareTag("Key")) { if (Input.GetKeyUp(KeyCode.Return)) { KeyController keyController = hit.collider.GetComponent <KeyController>(); keyController.LoadStage(); } } if (hit.collider.CompareTag("Goal") && enemyController.enemyCount == 0) { GoalController goalController = hit.collider.GetComponent <GoalController>(); goalController.Goal(); } moveAmount.y = (hit.distance - skinWidth) * directionY; rayLength = hit.distance; if (collisions.climbingSlope) { moveAmount.x = moveAmount.y / Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Sign(moveAmount.x); } collisions.below = directionY == -1; collisions.above = directionY == 1; } } if (collisions.climbingSlope) { float directionX = collisions.faceDir; rayLength = Mathf.Abs(moveAmount.x) * skinWidth; if (Mathf.Abs(moveAmount.x) < skinWidth) { rayLength = 2 * skinWidth; } Vector2 rayOrigin = ((directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight) + Vector2.up * moveAmount.y; RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask); if (hit) { float slopeAngle = Vector2.Angle(hit.normal, Vector2.up); if (slopeAngle != collisions.slopeAngle) { moveAmount.x = (hit.distance - skinWidth) * directionX; collisions.slopeAngle = slopeAngle; collisions.slopeNormal = hit.normal; } } } }
void HorizontalCollisions(ref Vector2 moveAmount) { float directionX = Mathf.Sign(moveAmount.x); float rayLength = Mathf.Abs(moveAmount.x) + skinWidth; for (int i = 0; i < horizontalRayCount; i++) { Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight; rayOrigin += Vector2.up * (horizontalRaySpacing * i); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask); Debug.DrawRay(rayOrigin, Vector2.right * directionX, Color.red); if (hit) { if (hit.collider.CompareTag("Coin")) { CoinController coinController = hit.collider.GetComponent <CoinController>(); coinController.GetCoin(); Destroy(hit.collider.gameObject); } if (hit.collider.CompareTag("Enemy") && !enemyController.isGameOver) { Debug.Log("GameOver"); enemyController.gameOver(); } if (hit.collider.CompareTag("Goal") && enemyController.enemyCount == 0) { GoalController goalController = hit.collider.GetComponent <GoalController>(); if (!goalController.isGoal) { goalController.Goal(); } } if (hit.distance == 0) { continue; } float slopeAngle = Vector2.Angle(hit.normal, Vector2.up); if (i == 0 && slopeAngle <= maxSlopeAngle) { if (collisions.descendingSlope) { collisions.descendingSlope = false; moveAmount = collisions.moveAmountOld; } ClimbSlope(ref moveAmount, slopeAngle, hit.normal); } if (!collisions.climbingSlope || slopeAngle > 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; } } } }