- Home /
Simple 2D procedural galaxy map
Hello everyone. I'm new to unity, a bit less to programming in general (but far from being an expert).
For a long period, i was in my free time trying to develop a 4x space civilization management game in c# with visual studio. For the last few days i was trying to convert my game to a unity project, but i'm struggling with somes aspects.
I'm liking Unity so far, but that editor make me feel way more stupider that i am (i hope). For example, i'm currently stuck on the galaxy map. What i want is simple enough, just a canvas, which i can change the size with my code (since it depend on the galaxy size parameter), contained in a scrollview so i can zoom and navigate in the map.
Why can't i simply draw my systemDot (simple sprite render with a white dot) where i want ?
Here is the code :
galaxyMap.sizeDelta = new Vector2(galaxy.Size, galaxy.Size);
foreach(StarSystem system in galaxy.Systems)
{
Vector2 position = system.Coordinates/100f;
Instantiate(systemDot, position, transform.rotation, galaxyMap);
Text systemName = Instantiate(systemNameText, position, transform.rotation, galaxyMap);
systemName.text = system.Name;
}
galaxyMap is a RectTransform attached to a panel. I don't know if that the thing to do to set the size of the content of the viewport of a scrollview, but it seems to work.
What i cant get to work is the pixels coordinates.
In the GalaxyGenerator code, the coordinates range from -1000 to 1000, as integer. If i simply instantiate my systemDot with these coordinates, i can't find my point anywhere. But, if i divide by 100f, i start to see my galaxy, but some of the systemDot are out of the galaxyMap. Heck, a simple instantiate with 0,0 as coordinates and the galaxyMap as parent doesn't even show on the map.
Do someone has an idea or the right way to generate the sprite where i want ? Sorry if it's not that clear, to be honest, the whole pixels management in unity is still not to me (i know there are screenPoint, worldPoint, rectPoint... i tried some of these but i had no simple solution for what seems to be a fairly simple problem.)
Here is my hierarchy :
My canvas is a screen space camera (i don't know if that important) with constant pixel size and my camera is orthographic.
Answer by Shifty-Geezer · Aug 03, 2017 at 02:33 PM
I'd ask why you're using a canvas to draw? That's for user interface stuff like buttons. For a map, I'd use a texture on a quad or sprite. You also shouldn't be using pixel coordinates because these will change depending on platform. Unity doesn't work in screen space using pixels like a conventional 2D approach. Use world-space coordinates and set the camera to view it.
From the sounds of it, I think you could do with stepping back from your port and just doing a few tutorials on Unity to see how it's supposed to work. Then you can come back to your 4X conversion with a good understanding on how differently you may have to do things. Perhaps have one project just dabbling where you learn a technique, then back to 4X to implement it. That's what I do exploring something new for my game.
You were right :) I Followed a new tutorial on grid mesh creation (here it is, lot of nice quality tutorials : http://catlikecoding.com/unity/tutorials/) and i was totally going the wrong way, co$$anonymous$$g from visual studio (where you draw on a canvas).
I used a new simple scene with a camera looking straight at a sprite background and before that background, an empty gameobject containing my dots generation system. After that, i simply looked up the net for some zoo$$anonymous$$g and drag scrolling scripts for the camera, and everything works as intended.
As you said, no need for pixels, nor canvas, it was a bit of a revelation, since it's not the way i'm used to code :) .
Answer by Whiden · Aug 03, 2017 at 03:06 PM
Even if the map is just some sprites object on a image background ? I used canvas cause it seemed the only object to be shown to the scene (the scrollview doesn't show if not under a canvas).
I don't quite get what you mean by using world-space coordinates instead of pixels. What should i do with say, a system with x:1400 and y:600 as coordinates, for example ?
You're quite right, i'm fairly underlevel in unity. I already followed easy tutorial but it sure is not enough right now, but i quite like to challenge myself by trying on my own, even though on this case, i'm lost.
Do you think Unity would be a good editor for a full 2D UI game without graphic anyway? The 4x game i'm making has only a galaxymap with dots and a systemmap with simple objects, the rest is complete UI (menu, tabs and labels, no graphic at all). Visual studio seems to offer a better range of UI component (tabview, listview), but unity is orientated toward game creation, and may offer already done aspect, such as map scrolling by drag (i had to implement it myself in studio).
Unity can absolutely make a 4X game. You just need to stop thinking in terms of pixels. ;) eg. You could have a thousands star sprites at coordinates ranging from (-700,-300) to (700,300) and have a camera that can zoom in and out on any part of that map. And you can set this up so it works on anything from a 1024x768 4:3 aspect iPad to a 1920x1080 16:9 console or PC to a 2736x1824 3:2 Surface Pro.
Then you can create your UI over the top. I personally use NGUI as way faster than Unity's inbuilt UI, but both have extreme learning curves.