Please help with MiniMap...

I am creating a simulation of a retail plaza. Many of these scripts/tools I am trying to get working will be used on all my projects. I do not yet have my lightmaps in the scene nor is the model completely finished. I will also have mocap people walking about.

That being said, I am having some difficulty with getting a minimap working. I have gone through the lessons and the forums and couldn’t find much to help me. I am attaching 2 images that shows the problem I am having. In the upper left of the second image you can see the minimap is doing some strange things. I have an orthographic camera set point downwards and is parented to the FPS camera. It should be pointing downward but I am still getting some GUI ‘overlap’(for lack of a better term).

Second I have a help Gui showing the controls for my gamepad. I received some help from Targos, but I can’t seem to get the code to work to have it toggle the gui texture on/off with the h key on the keyboard.

var isHudOn : boolean = true; 

function Update () 
{ 
   if (Input.GetButtonDown("HUDControl")  isHudOn == true) 
   { 
      guiElement.enabled = false; 
   } 
   else 
   { 
      guiElement.enabled = true; 
   } 
}


sorry, that was code i posted in error, this should work:

var isHudOn : boolean = true; 

function Update () 
{ 
   if (Input.GetButtonDown("HUDControl")
   {
      if (isHudOn == true) 
      { 
         guiElement.enabled = false; 
      } 
      else 
      { 
         guiElement.enabled = true; 
      } 
   }
}

I am now receiving this error after inputting your code.

Assets/Stanadard Assets/Scripts/HelpKey_toggle.js(16) error BCE0044: expecting EOF, found “}”.

Here is the code in the script I named HelpKey_Toggle.js

var isHudOn : boolean = true; 

function Update () 
{ 
   if (Input.GetButtonDown("HUDControl") 
   { 
      if (isHudOn == true) 
      { 
         guiElement.enabled = false; 
      } 
      else 
      { 
         guiElement.enabled = true; 
      } 
   } 
}

Thanks for any continued help! EDITED code to show properly v in var as lower case.

Gary

The “Var” on line 1 should be “var” (lowercase “v” --Javascript is case-sensitive)

Freyr it is. Sorry my bad when I pasted it I missed the first letter(so I typed it in and had caps on) and it is actually var. Same error anyway.

There is also a missing “)” at the end of line 5: if (Input.GetButtonDown(“HUDControl”)

dammit ; )

sorry gary for the silly errors i should have tested it in unity i just didn’t have the time last night. my desire to contribute is much larger than my coding ability - i’m getting better but i’m still not above some silly mistakes. i’ll test my code from now on if i post again ; )

O.K. I see the error now. Good no errors in the console now. I have the HUDControl assigned to Element 17 of the input manager. The Gui is ‘live’ and then when I hit the h key it disappears. Good! It does not reappear when I hit the h key again. I have h mapped to the positive button in Element 17 of the input manager. So, we are getting much closer.

I would like to have the gui disabled until the user hits the h key first. I thought that was what the code was doing. Anyway, this is starting to make sense now. If I could get the key to toggle it on then off it would be much better :wink:

Dr. Jones. No problem with the error! I am just learning this code stuff so I will be missing lots of little things for awhile. Thanks again and we are getting much closer now!

Cheers,

Gary

You’re setting isHudOn to true in the first line. Unless there’s some code elsewhere that changes it, it’s always true, therefore the line “if (isHudOn == true)” will always be true. (BTW, this can be more efficently written as “if (isHudOn)”, which does the same thing.) So the “else” line will never execute. I don’t really see a need for the variable at all, though. You can just toggle the guiElement directly. How about this:

function Update () { 
   if (Input.GetButtonDown("HUDControl")) { 
      if (guiElement.enabled) { 
         guiElement.enabled = false;
      } 
      else { 
         guiElement.enabled = true; 
      } 
   } 
}

If you want it so it’s invisible until hitting “h”, just set it to not enabled in the editor; the code doesn’t change.

–Eric

Eric,

AWESOME! It works flawlessly now! Now I just need some help with the MiniMap issue from the first page.

The Otho camera(overhead Camera) camera is currently parented to the FPS camera and is pointing downward to show a ‘plan’ of sorts. It has a rendertotexture applied to it and then the FPS camera has that placed as a Guitexture in the upper left of the users screen.

The problem arises in that when the h key is pushed and the help Gui texture is on it shows itself twice in the rendertotexture. As in the second image. When I turn it off you can still see the other Gui texture shown twice(which I don’t want to see at all in the minimap) and the minimap is almost completely transparent.

Thanks for the help from everyone!!!

Cheers,

Gary

Actually, I wasn’t really thinking about what I wrote. Since it’s just a simple toggle of the guiElement, you can use this instead:

function Update () {
	if (Input.GetButtonDown("HUDControl")) { 
		guiElement.enabled = !guiElement.enabled;
	}
}

The “!” means “not”. So it sets guiElement to be the opposite of whatever it is now. Obviously both methods work…but one is more efficient…

As to the minimap, I’m not really sure. Do you even need to use render to texture for that? I’d think you could use the normalized view point rectangle for the secondary camera to make the minimap directly instead of the RTT/GUITexture thing, but I haven’t messed with minimaps yet so I’m not 100% sure.

–Eric

Eric thanks again!

Is there anyway to have the Gui fade on and off when hitting the toggle key?

Not sure what the heck you were talking about on the minimap stuff(right over my head)!

Cheers,

Gary

I still can’t seem to get the minimap working… Is there a quick workflow someone could help me with? Would be nice to tie it to a hotkey on/off as well.

Thanks for your time.

Gary

the 2x gui is to do with layers and culling, I would guess. on the minimap camera, turn off the layer where the gui is.

You can fade in guis. On the wiki theres a gui fade script. I havent used it so cant guide you there.

Theres an example of layered cameras, ie a compass overlay which is a similar concept to a minimap on the “mountable jeep” thread.

Tying it to a hotkey is a clone of the switch cameras script which is also in the unitypackage on “mountable jeep”(search forum for it)-you’ll see the compass is not in every camera window, only the ones I wanted, as defined by the “Camera Updater” script. What Eric was talking about is what is in that package…I used an ortho camera on the compass. Download it and fiddle round…its all about layers, what renders on top of what…

Thanks Joachim, Dr Jones and Eric for making sure Gary gets clean code in the end, I didnt mean to pass code with errors, thats just frustrating to the origional poster, I appreciate all contributions towards ironing out the wrinkles…
AC
PS our phone gets cut off in the next few days, though I may be able to access forum from school, depending on the filters that stop students from accessing “games” releated sites…Good luck Gazza!
AC

I figured out why everything was nearly transparent! I had altered the alphas on the Main color for each material, becuase I was going to use glow and was controlling the glow via the alpha channel. Now everything is opaque(or nearly)!

Double Gui solved by Targos hint! I removed the gui component from the overhead camera.

One more question on minimaps. Can I FORCE the minimap to not rotate based on the fps camera since the overhead camera is parented to the fps camera?

I really don’t want rotation only translation. Thank you everybody!!!

Cheers,

Gary

There’s no built-in function for that, so you have to code it, so it will be slightly more complex than two lines. :wink: Of course, you can do whatever you want with code. There are any number of ways to do it…you’d need a fading routine for the alpha value of the gui texture, and some variables to tell which direction to fade, how fast, and whether the fade routine should be going.

Well, let’s just do it:

var fadeSpeed : float = 1;
var maxFade : float = 1;
private var opacity : float = 0;
private var isFading : boolean = false;
private var fadeDirection : int = -1;

“fadeSpeed” is pretty self-explanatory…bigger for a faster fade, smaller for a slower fade. “maxFade” will be how much it fades in…1 is 100% opaque, but you could set it to something less. Like .5 if you wanted it to fade in to a max of 50%. The private variables won’t be exposed because they don’t need fiddling with in the inspector. “opacity” will be the current amount of the alpha value at any given time, “isFading” says whether the fade routine should be running, and “fadeDirection” says whether it should fade in or out…-1 would be going down (out) and 1 would be going up (in).

Then we’ll have a start routine, so the code will work if the gui texture is enabled. Right now the variables are set up so the code will work right only if the gui texture is disabled to begin with, so let’s fix that:

if (guiElement.enabled) {
	fadeDirection = 1;
}
opacity = guiElement.color.a;

We need an initial value for “fadeDirection”, and since it gets toggled, it should be the opposite of what we’ll want to start out with. We want it to go up for fading in and down for fading out. “opacity” is set to the alpha value of the color, so if you start with the gui texture disabled, you should also set the alpha to 0. Otherwise, the HUD would “sproing” into view the first time instead of fading in. On the other hand, if you start with the gui texture enabled, then the alpha of the color should be set to the same value as “maxFade”.

Then, for the update routine:

if (Input.GetButtonDown("HUDControl")) {
	fadeDirection = -fadeDirection;
	isFading = true;
	guiElement.enabled = true;
}

Check to see if the user hits the HUD control key/button. If so, toggle “fadeDirection” and set “isFading” to be true so the fade routine will run. Also, turn the guiElement on. This part could be omitted, because if you have a 0% opacity, you won’t see the HUD. But my sensibilities say that it should be turned off entirely because that’s a bit faster…if the opacity is 0, it still has to be drawn even if you don’t see anything.

This works so that the user can toggle the HUD while it’s still in the middle of fading in or out. Probably it would be potentially annoying if you waited until it was fully faded in/out before allowing a toggle.

Also in the update:

if (isFading) {
	opacity += fadeDirection * fadeSpeed * Time.deltaTime;

If “isFading” is set, then fiddle with the “opacity” value. We want to multiply “fadeDirection” by “fadeSpeed” to either increase or decrease the value, depending on whether “fadeDirection” is 1 or -1, and then multiply by Time.deltaTime so it’s framerate independent.

if (opacity < 0) {
	opacity = 0;
	isFading = false;
	guiElement.enabled = false;
}

Once we’ve changed “opacity”, then check to see if it’s gone below 0. If so, first explicity set it to 0. That’s because it’s possible, depending on the frame rate, for the value to end up enough below 0 that the next time you toggle the HUD and the “opacity += fadeDirection * fadeSpeed * Time.deltaTime;” line runs, it will still be below 0, so it will never fade in again. And that would be bad. :wink: Then set “isFading” to false, so the fading routine won’t run the next Update. Finally turn off the guiElement altogether (the reason for that explained above).

if (opacity > maxFade) {
	opacity = maxFade;
	isFading = false;
}

Now check to see if “opacity” ended up above our “maxFade” value instead, in which case explicitly set the value to maxFade (same reason as setting it to 0), and set “isFading” to false so the fade routine stops. No need to set guiElement.enabled to true, since we already did that when you hit the HUD toggle button.

And finally:

guiElement.color.a = opacity;

That sets the alpha value of the guiElement to whatever “opacity” is this frame.

All together now:

var fadeSpeed : float = 1;
var maxFade : float = 1;
private var opacity : float = 0;
private var isFading : boolean = false;
private var fadeDirection : int = -1;

function Start () {
	if (guiElement.enabled) {
		fadeDirection = 1;
	}
	opacity = guiElement.color.a;
}

function Update () {
	if (Input.GetButtonDown("HUDControl")) {
		fadeDirection = -fadeDirection;
		isFading = true;
		guiElement.enabled = true;
	}
	
	if (isFading) {
		opacity += fadeDirection * fadeSpeed * Time.deltaTime;
		if (opacity < 0) {
			opacity = 0;
			isFading = false;
			guiElement.enabled = false;
		}
		if (opacity > maxFade) {
			opacity = maxFade;
			isFading = false;
		}
		guiElement.color.a = opacity;
	}
}

Sorry. :slight_smile: If you look at the camera values in the inspector, you’ll see “Normalized View Port Rect”, and four values for that: Xmin, Ymin, Xmax, Ymax. Those set the relative coordinates for, respectively, how far in from the left side to start rendering, how far up from the bottom, how far from the left side to stop rendering, and how far up from the bottom to stop rendering.

So a little square in the top left corner would have values of 0, .7, .2, and 1 (or at least, that results in a square on a 4:3 monitor). So you can have a second camera with those values, and set the Depth to something greater than the depth of the main camera so it appears on top (the default is 0, so have the depth of the main camera be 0 and the depth of the secondary be 1).

You wouldn’t want the minimap camera to be parented to the main camera, because then it would rotate when the player looked up and down and stuff. So you’d just make the camera be a seperate object on its own, and attach this script:

var objectToFollow : Transform;
var distanceAbove : float = 100;

function Update () {
	transform.position = objectToFollow.position + Vector3(0, distanceAbove, 0);
}

It doesn’t matter what the position is since the script sets that. But change the rotation so it’s rotated 90 degrees on the X axis and 0 for Y and Z; that way it points down.

Then drag an object from the first person controller (if that’s what you’re using) to the “objectToFollow” slot, and change distanceAbove to whatever value works best for how high up the minimap camera should be.

Of course, that’s pretty quick&dirty. One possible problem is that if the player goes up, the minimap zooms out (and zooms in if the player goes down). But maybe that’s desirable. If not, that’s easy enough to fix, just by using the X and Z values from “objectToFollow” and setting the Y value to something constant.

Oh yeah…and turn the GUILayer off for the minimap or remove it. Otherwise the HUD stuff would show up. I suspect that might be the problem you were running into even with the render to texture method. :slight_smile: But this way might be a bit faster (and it works with Indie as well as Pro).

–Eric

Ha

I am an anaconda, I live on code. When Eric throws food into our pen, I can swallow it to my hard drive, and then by not moving for a couple of weeks, I can slowly digest it, living on its sustenanace…

Great stuff man, I cant move… :smile:
AC

Aw man! I just got schooled :wink: Thank you so much! I can see many derivatives and alternate uses for the code you created above. I am in your debt. You need help with content sometime you let me know. Since I have no skill(z) with the coding!!! Thank you so very much. This community truly is the BEST I have ever dealt with in the 15+ years I have been doing CG!

Cheers,

Gary

You’re welcome; I see you and I posted at nearly the same time before. Sorry 'bout that. At least I was right about the GUILayer thing. :wink:

On second thought regarding that code, I’d use this for the Start function instead:

function Start () { 
   if (guiElement.enabled) { 
      fadeDirection = 1;
      opacity = maxFade;
   } 
   else {
      opacity = 0;
   }
}

That way you’d never have to fiddle with the opacity in the inspector…slightly more elegant I think. And for completeness, here’s a camera-follow script for the minimap that doesn’t go up and down with the player and stays at a fixed height:

var objectToFollow : Transform; 
var distanceAbove : float = 100; 

function Update () { 
   transform.position = objectToFollow.position;
   transform.position.y = distanceAbove;
}

–Eric

Sorry…but that sounded like a challenge to me.

if (Input.GetKeyDown("t")) visible = !visible;
renderer.color.a = Mathf.Lerp(renderer.color.a, (visible ? 1.0, 0.0), 0.1);

I guess that could be cheating a bit. you’d also need to declare visible as a variable, which one might consider a third line.

Don’t mean to threadjack. Just wanted to show… if you want to squeeze functionality down, it can ALWAYS become smaller :wink: I’m sure there’s a way that my first line there could somehow be integrated into the second.