- Home /
Image-based Level Generator having problems
Alright so I've built a custom level generator that generates a level based off of a randomly chosen Texture2D in the game files. These textures are 140x80 and are highly colorful. The level generator reads the files, then spawns objects based on the color in the image and spawns them in the world. Note that my game is an entirely top-down 2D game, with an Orthographic camera that does not move.
Now this works fine and dandy for the primary device I am developing for (iPhone 5, 1136x640 resolution), but I get problems when I play on other devices: The way I am spawning the objects is based on the Camera, by using the position of the pixel in the image as the position to spawn the object in the World.
In a singleplayer game this would be fine, because you would be the only one seeing everything and all NPCs and things would respect the definitions you've set because everything revolves around you. But my game is a Multiplayer game.
So a quick example of the problem... A player joins a server on an iPhone 5 (1136x640), the arena is spawned based on parameters sent by the Server on connection, and begins moving around. Another player joins using an iPad 3 (2048x1536), spawns the arena based on parameters sent by the Server, and then starts moving around. So the iPhone5 user sees the arena based on their aspect ratio (16:9), while the iPad3 user sees the arena based on their aspect ratio (4:3). On one screen (iPhone5), the player could be moving left+down through a hallway, but on the other screen (iPad3), that player seems to be walking into the wall just below/above the hallway as though stuck, because the map has been spawned slightly differently because of the width/height of the screen.
If anybody could help, that would be great. The entire resolution/aspect ratio/device thing is incredibly confusing to me, and I would dearly like to finish up this level generator and move onto more gameplay-oriented code.
The background of the arena is 1136x852, which perfectly fits all aspect ratios I am trying to support (16:9, 4:3, 3:2) when I zoom out the camera slightly to fit in the proper width. The main problem is with the height, which auto-adjusts based on the resolution.
ArenaGenerator.js
#pragma strict
var environmentColors: Color[]; //THE COLORS TO BE READ
var environmentObjects: GameObject[]; //OBJECTS TO BE SPAWNED FOR THE COLORS
var environmentImages: Texture2D[]; //THE ARENA TEXTURES THEMSELVES
function SpawnTheArena() {
var setScreenWidth: float = 1136;
var setScreenHeight: float = 640;
var diffScreenWidth: float = Camera.main.pixelWidth/1136;
var diffScreenHeight: float = Camera.main.pixelHeight/640;
var chosenArenaID: int = 0; //SET BY THE SERVER THROUGH RPC CALLS NOT INCLUDED IN THIS SNIPPET
for (var w:float=0;w<environmentImages[chosenArenaID].width;w++) {
for (var h:float=0;h<environmentImages[chosenArenaID].height;h++) {
var thisPixel = environmentImages[chosenArenaID].GetPixel(w, h);
var xPos: float = (w/environmentImages[chosenArenaID].width)*setScreenWidth*diffScreenWidth;
var yPos: float = (h/environmentImages[chosenArenaID].height)*setScreenHeight*diffScreenHeight;
var thisPixelSpot: Vector3 = Camera.main.ScreenToWorldPoint(Vector3(xPos,yPos,0));
//MY COLOR READING AND SPAWNING CODE GOES HERE BASED ON THE VECTOR3 ABOVE.
}
};
};
}
Any help would be very much appreciated. Thank you!