- Home /
How to create a 2d game grid background for my game?
I have created a game using html/css/javascript . I want to create it for Unity using the same logic. Its a 2d game based on a grid background with some moving objects in grid cells and when the actor object moves camera also should be moving till the background edges.
I just need some simple steps how to create scene including a camera, background grid (cells) objects. I tried using an UI element with a canvas but I am stuck at placing each cell at correct place. I used below code:
private void PlaceBoxs()
{
boxes = new Box[xDim, yDim];
for (int x = 0; x < 1; x++)
{
for (int y = 0; y < 1; y++)
{
//// set background
// clone box cell prefab
GameObject bgObject = (GameObject)Instantiate(prefabDict[PrefabType.Box], GetBoxWorldPosition(x, y), Quaternion.identity);
// set grid as parent for cloned prefab
bgObject.transform.parent = transform;
}
}
}
public Vector2 GetBoxWorldPosition(int x, int y)
{
return new Vector2(transform.position.x - xDim / 2.0f + (x * 0.55f) + 2f, transform.position.x + yDim / 2.0f - (y * 0.55f) - .653f);
}
this code is written in parent sprite, my problem is I am not able to calculate world position for each cell in above method (`GetBoxWorldPosition`) I am just doing digits plus minus to set their positions but I don't know a proper formula to place them at their correct positions. Also currently the scene hierarchy is right now I am not using any UI element or canvas its just a 2 level hierarchy, Is correct? or any changes needed?
Your answer
Follow this Question
Related Questions
How do I move a 2d object to center of screen? 1 Answer
Collect pickup if mouse button is not pressed 0 Answers
2D EndlessRunner Spike Placing 0 Answers
Dictionary in second script empty 1 Answer