- Home /
Problem with Minimap.
I've made a HUD for my game, which has a space for the minimap, which is just a camera pointing down to the player.
I'm trying to make it so that the minimap is always within that space and the same size, regardless of resolution. Right now, whenever I play it maximised, the Minimap gets too big, and is outside of the HUD.
How do I fix this?
EDIT: Here's my code:
public float desiredWidth;
public float desiredHeight;
public float desiredX;
public float desiredY;
void Awake()
{
float nWidth = desiredWidth / Screen.width;
float nHeight = desiredHeight / Screen.height;
float nX = desiredX;
float nY = desiredY;
gameObject.camera.rect = new Rect(nX, nY, nWidth, nHeight);
}
On the top left.
I'm using the actual transform to move the HUD about, but for the camera, I'm using the position variables on the camera component in the $$anonymous$$imap to move and scale that.
What does your hud do when the resolution changes, does it stretch? Or does it simply reposition elements?
It repositions the HUD Image, and the camera/$$anonymous$$imap isn't visible anymore, it's too small.
Answer by kolmich · Jan 30, 2013 at 10:05 AM
Hi Cobra!
We have created the KGFMapSystem (unity3d minimap) and after very long discussions we also decided to control the size of our minimap in normalized coordinates (0-1 relative to the screen widht) instead of making it pixelperfect.
The reason for this is that we think that creating pixelperfect huds is not a good idea anymore since apple invented the retina display where your 64x64 pixel icon will simply shrink into a point.
So i know that this is not the answer to your question, but a possible solution would be to make sure that your hud will also resize relative to the screensize so the hud will always look the same, on all resolutions (even on a retina display) and keep the minimap sizing as it is?
Michal
How do I make the HUD resize depending on resolution?
Hi Cobra!
There are 2 ways of doing this. One way would be to really make all your gui controls relative to the Screen size or width.
For example ins$$anonymous$$d of drawing a button using width = 200 you can draw a button using width = Screen.width/5.0f. In such a way this button will always take the same amount of space on all kind of displays.
An other way would be to design your complete gui for a fixed screen size like e.g. 1024x768 and then stretch it to fit into bigger screens using GUI.matrix or GUIUtility.ScaleAroundPivot. The tricky part here is to react correctly on aspect ratio changes.
Or as i've just descovered, set in the inspector of your GUITexture, get the pixel inset, and change all it's values to 0, it did the trick!
By the way, is there anyway to make the map draw after the HUD, or put it in a layer above? I want to make absolute sure that the map won't be appearing outside the HUD.
Hi Cobra! Sorry i do not understand what you mean with "make the map draw after the $$anonymous$$imap" cause i don't know how your hud/$$anonymous$$imap looks like..
Do you mean you want to draw the gui over the $$anonymous$$imap camera viewport or otherwise?
Answer by Doireth · Jan 29, 2013 at 09:35 PM
Adjusting (by script) the width and height of your minimap camera by dividing the screen width and height to your needs. That way the minimap will always be proportional to the screen.
http://docs.unity3d.com/Documentation/ScriptReference/Screen.html
Not sure I follow?
Would I do it like this on Awake (or Start), I would divide the map's current width and height by the screens resolution?
Would I also do the same thing with position?
In Update(). FatWednesdays answer is much better though. I forgot about normalized camera rects.
Do not put the code in update. Doing so will mean your camera is constantly reseting its size and position even when it doesnt need to. Sure you might get away with this on games that arent processing intensive, but its bad practice. If your game is always maximized, and or does not resize after its started, then put the code for this on awake or start. If your game screen does resize at runtime, or swap between fullscreen modes that resize it, only update the camera data once, when the resize occurs.
Answer by FatWednesday · Jan 29, 2013 at 09:38 PM
By default, the values you set in the inspector are Normalized position in screen space [0-1] This means that what you see will be different on each resolution because the screen size is different.
So what you need to do if you want a fixed size, if write a script that has a calculation in the awake or start method that calculates the normalized values for the size and position based on the screen size. These calculations for the new normalized size should do the trick.
float nWidth = desiredWidth / Screen.width;
float nHeight = desiredHeight / Screen.height;
working out the position will depend on how you want it positioned, aligned to the side, distance from center, etc. So i'll leave you to work out the calculations for that one.
and by desired width and height, i mean the value in pixels.
So for example, $$anonymous$$imap's position is set to, say, 1 or less on X and Y, would you still divide them by resolution, yeah?
The rect for the camera defines normalized coordinates, this means that 0 is left, 1 is right, so your position should not be 1 unless you want the camera offscreen. If you want the camera always pinned to the top right for instance you would calculate its width and height as i showed above, and then its position would become.
X = Screen.width - nWidth. And Y = 0.
If you play around with the values in the inspector field "normalized viewport rect" on camera you should get a feel for what those values do.
Okay, I've done what you said in the script, I doubt the nWidth and nHeight variables alone can change the objects scale, what do I set as nWidth and nHeight?
If by aligned to the side, you mean starting position with be the very side, and any bigger/smaller would distance it from the side, I would like the position to be aligned from the left, as it will be on the top-left, how do I go about doing the same/similar thing as the scale?
Of course they will change its scale, the width and height values of something are what deter$$anonymous$$e a 2D objects scale.
nWidth and nHeight are values you have to calculate, check my original answer for how to calculate those.
If the map will always be anchored to the top left, then your position will always be 0,0. even normalized. (in normalized coordinates 0 is left, 1 is right, and 0 is top, and 1 is bottom).
Your answer
Follow this Question
Related Questions
Fixing the camera's left side in a 2D Game in all screen resolutions 0 Answers
Distribute terrain in zones 3 Answers
Put player at center-left on the camera 0 Answers
What resolution should my game be? 2 Answers