Scaling grid around camera position

Okay, so the goal here is to create the illusion of altitude on a tile map by scaling the grid smaller the higher you are.

Currently I scale the cell size of the grid that the tile map is attached to in order to maintain the cell structure at all scales (as opposed to scaling the parent transform)

This works fine but the tile map then scales with the bottom left corner being the origin. Thus a tile map with a cell count of 1000, 1000 and a cell size of 1 would mean the top right boundary is at 500,500 (world). When scaled in half the top right boundary falls at 250,250 (world).

My goal is to move the bottom left corner (the origin) of the tile map object in order to maintain the cameras position relative to the tile map throughout scaling. If I didn’t do this position of the camera would translate along the tile map as it scales.

Since the goal is to give the illusion of gaining or losing altitude its important that each time the tile map is scaled the camera remains at the same relative location on the tile map. Thus the map has to move with the scaling.

The way I’ve been approaching this is by grabbing the top right world position of the tile map before and after the world is scaled. Then grabbing the relative position of the camera to the top right before scaling and using that to determine where the camera should be after scaling. Then using the difference between where the camera is now and where it should be for the current scaling to offset the origin of the map. (Yes I know this is rather confusing).

Vector3 centerPos = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, 0);

            Vector3 preLoc = tGrid.CellToWorld(new Vector3Int(tileMaster.tileMaxWidth, tileMaster.tileMaxHeight, 0));

            float altDif = ((float)shipAlt / (float)shipAltMax);
            float invDif = Mathf.Clamp(1 - altDif, 0.01f, 1);

            if (altDif != lastAltDiff)
                tGrid.cellSize = new Vector3(invDif, invDif, 1);

            Vector3 postLoc = tGrid.CellToWorld(new Vector3Int(tileMaster.tileMaxWidth, tileMaster.tileMaxHeight, 0));

            float apx = centerPos.x / preLoc.x;
            float apy = centerPos.y / preLoc.y;

            float ppx = postLoc.x * apx;
            float ppy = postLoc.y * apy;

            float cAdjX = centerPos.x - ppx;
            float cAdjY = centerPos.y - ppy;

            if (altDif != lastAltDiff)
            {
                Debug.Log("TR Pre Pos: " + preLoc.ToString() + " | TR Post Pos: " + postLoc.ToString() + " | Camera From Corer %: " + apx.ToString() + "," + apy.ToString()
                    + "| Old Camera Loc: " + centerPos.ToString() + " | New Camera Loc: (" + ppx.ToString() + ", " + ppy.ToString() + ", 0) | Camera moved by: (" + cAdjX.ToString() + ", " + cAdjY.ToString() + ", 0)");

                transform.Translate(new Vector3(cAdjX, cAdjY, 0));
            }

            lastAltDiff = altDif;

Ignore the messiness as this was me painstakingly trying stuff.
The result is close but it results more in some weird logarithmic affect and the accuracy is good close to the ground an bad high up.

The scaling is determined by the percentage of altitude to max alt.

Perhaps I’m thinking about this all wrong. Would love feedback.

Thanks!
Zach

Have you tried using an orthographic camera and adjusting camera.orthographicSize? It’s not clear to me how your intention - which perhaps I’m misinterpreting - is different than that.

No you’re correct that would work. I just failed to mention that there is a ship that the camera is following that cannot decrease or increase in size with the rest of the scene. =)

Come to think of it. It would probably make more sense to go the orthographic size route and just scale the ship. =?

That makes perfect sense and seems simpler.