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.
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:
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
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);
}
}