MINING QUEST: ‘Big and Small’ - Solutions

Quest: Mining Quest
Challenge: Big and Small

Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.

It really feels good to find solutions for these challenges. There was a lot to do this time:

  • Put in variables for the size and a random min and max
  • Calculate the center starting position for Mr. Beardy
  • If the size changes Mr. Beardy needs to be moved accordingly. Because I instantiated him, the camera needs to be hooked up per script
  • And surprise, the borders won’t fit the new size, so they were also prefabbed and sized dynamically. I took a pencil and some paper to find the correct parameters for that.

Here is some of the interesting code:

    private void SpawnMisterBeardy() 
    {
        Vector2 beardyLocation = new Vector2(centerLocationX, centerLocationY);
        GameObject mrBeardy = Instantiate(playerPrefab, beardyLocation, Quaternion.identity);
        DestroyRocksAt(beardyLocation);

        cinemachineCamera.m_AnimatedTarget = mrBeardy.GetComponent<Animator>();
        cinemachineCamera.Follow = mrBeardy.transform;
    }

    private void SpawnBorders()
    {
        GameObject leftBorder = Instantiate(borderPrefab, Vector3.zero, Quaternion.identity);
        GameObject rightBorder = Instantiate(borderPrefab, Vector3.zero, Quaternion.identity);
        GameObject bottomBorder = Instantiate(borderPrefab, Vector3.zero, Quaternion.identity);
        GameObject topBorder = Instantiate(borderPrefab, Vector3.zero, Quaternion.identity);

        ResizeBorder(leftBorder, -1, (ySize/2f)-0.5f, 1, ySize);
        ResizeBorder(rightBorder, xSize, (ySize / 2f) - 0.5f, 1, ySize);
        ResizeBorder(bottomBorder, (xSize / 2f) - 0.5f, -1, xSize, 1);
        ResizeBorder(topBorder, (xSize / 2f) - 0.5f, ySize, xSize, 1);
    }

    private static void ResizeBorder(GameObject border, float offsetX, float offsetY, float sizeX, float sizeY)
    {
        border.GetComponent<BoxCollider2D>().offset = new Vector2(offsetX, offsetY);
        border.GetComponent<BoxCollider2D>().size = new Vector2(sizeX, sizeY);
    }

Please let me know what you think :slight_smile:

1 Like