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:

2 Likes

My solution was pretty similar to @MC_dev, but I left the existing borders in the game and just resized them at runtime. Probably cleaner to prefab them and instantiate, but it works. I created a separate script that I attached to the Border parent object in the hierarchy:

public class BorderScale : MonoBehaviour
{
    int gridLength;
    [SerializeField] public BoxCollider2D left;
    [SerializeField] BoxCollider2D right;
    [SerializeField] BoxCollider2D bottom;
    [SerializeField] BoxCollider2D top;
    // Start is called before the first frame update
    void Start()
    {
        var environment = FindObjectOfType<GenerateEnvironment>();
        var environmentScript = environment.GetComponent<GenerateEnvironment>();
        gridLength = environmentScript.gridLength;
        GenerateBorders(gridLength);
    }
    void GenerateBorders(int gridLength)
    {
        left.offset = new Vector2(-1,gridLength/2);
        left.size = new Vector2(1,gridLength+1);

        right.offset = new Vector2(gridLength,gridLength/2);
        right.size = new Vector2(1,gridLength+1);

        bottom.offset = new Vector2(-1,gridLength/-2);
        bottom.size = new Vector2(1,gridLength+1);

        top.offset = new Vector2(gridLength,gridLength/-2);
        top.size = new Vector2(1,gridLength+1);
    }
}
1 Like

Privacy & Terms