コード例 #1
0
    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;
                }
            }
        }
    }