Map Suggestions, the way I did it may not be the best?

Quick explanation of what I did and why.
How can I do it better or what would be a better way of scripting a map?

I have a building that has multiple rooms 16. I put a trigger collider in each room (that has the script below attached to each trigger), so that when the player Clicks on the “M” key the map will popup Showing where the player currently is. The Problem Im noticing is when I click on the M key its not responding every frame (or quick enough)? And I dont really know of any other way to do it? So any suggestions would really be welcomed.

After getting this straightened out I also need a way to place a texture on the map of where I’ve been???

var DisplayMapBackground = false;
var MapSkin : GUISkin;
var MapBackground : Texture2D;

var youAreHere : Texture2D;
var missionEnd : Texture2D;
var beenHereBefore : Texture2D;//dont know how to do this yet???

var DisplayYouAreHere = false;
var DisplayMissionEnd = false;

var areHereX : int = 0;
var areHereY : int = 0;
var endX : int = 0;
var endY : int = 0;

function OnTriggerStay(Map : Collider)
{
	if(Map.gameObject.name == "Robot")   
	{
		if(Input.GetKeyDown(KeyCode.M))
		{
			DisplayMapBackground = true;
			DisplayYouAreHere = true;
			DisplayMissionEnd = true;
			//make other Gui Health bars to show false
		}	
	}
}
function OnGUI()
{
	GUI.skin = MapSkin;
	
	if( DisplayMapBackground )
    {
       
       //GUI Background Image KeyFound
		GUI.Label (Rect (0,0, 600,450), MapBackground, GUIStyle.none);       
       
       //pause game somehow
       
       //Ok Button Change the size 152 x 51  
   	 	if (GUI.Button (Rect (65, 390, 152,51), "","Ok"))
   	 	 {
   	 		DisplayMapBackground = false;
   	 		DisplayYouAreHere = false;
   	 		DisplayMissionEnd = false;   	 		
   	 		//unpausegame somehow
   	 		//show other gui health bars to true
   		 }	
   	 
   		if (GUI.Button (Rect (400, 390, 152,51), "", "Invite")) 
   	 	 {
   	 		//Intvite button code		
   		 }
   	}
	
	if(DisplayYouAreHere)
   	{
   		GUI.Label (Rect (areHereX,areHereY, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(DisplayMissionEnd)
   	{
   		GUI.Label (Rect (endX,endY, 29,29), missionEnd, GUIStyle.none);	
   	}
}

459382--16062--$Map.png

You can’t put Input.GetKeyDown code in physics functions like OnTriggerStay, because those functions run at the physics framerate. A key down event is only true for the single frame when it happens, which only sometimes happens at the same time that a physics frame runs.

–Eric

Ahhh… Any suggestions on another way of accomplishing this?

All I could think of was to have colliders or triggers to be able to know which room Im in?
I did have the map poping up when using Input.GetKeyDown which worked good in the update but then I had no idea how to overlay the youAreHere image and connecting that to where I was in the rooms.

How is it Normally Done?

Ok Looks like Im gonna need some help with this Please. My updated script is for when I click the M button the map shows up which works great :slight_smile:

I also have 3 icons to Overlay the map and represent where Im at, what the destination is, and where Ive been. How would I get the “where Im at” icon to show up depending on which room Im in?

I was thinking of using a trigger in each room but when I use a trigger the icon is automatically showing up (of course) and I dont know how to set it to only show up when Im in that room and only when the M key is pressed.

Does anyone know how I would do this? or another way altogether Im open to any suggestions.

Im trying my best its tough to find posts and resources for this.

Script for the M key and background image:

var DisplayMapBackground = false;
var MapSkin : GUISkin;
var MapBackground : Texture2D;

function OnGUI()
{
	GUI.skin = MapSkin;
	
	if(Input.GetKeyDown(KeyCode.M))
		{
			DisplayMapBackground = true;
			
			//make other Gui Health bars to show false because of the size of this map.
		}
		
	if( DisplayMapBackground )
    {
       
       	GUI.Label (Rect (0,0, 600,450), MapBackground, GUIStyle.none);       
       
       //pause game somehow
       
       //Ok Button Change the size 152 x 51  
   	 	if (GUI.Button (Rect (65, 390, 152,51), "","Ok"))
   	 	 {
   	 		DisplayMapBackground = false;
   	 		   	 		
   	 		//unpausegame somehow
   	 		//show other gui health bars to true
   		 }	
   	 
   		if (GUI.Button (Rect (400, 390, 152,51), "", "Invite")) 
   	 	 {
   	 		//Intvite button code		
   		 }
   	}
}

Script that I was using for the triggers in the rooms but may have to ditch:

var youAreHere : Texture2D;
var missionEnd : Texture2D;
var beenHereBefore : Texture2D;

var DisplayYouAreHere = false;
var DisplayMissionEnd = false;
var DisplayBeenHere = false;

var areHereX : int = 0;
var areHereY : int = 0;
var endX : int = 0;
var endY : int = 0;
var beenHereX : int = 0;
var beenHereY: int = 0;


function OnTriggerEnter(Map : Collider)
{
	if(Map.gameObject.name == "Robot")   
	{
		DisplayYouAreHere = true;
		DisplayMissionEnd = true;
	}
}
function OnTriggerExit(Map : Collider)
{
	if(Map.gameObject.name == "Robot")   
	{
		DisplayYouAreHere = false;
   	 	DisplayMissionEnd = false;
   	}
}
function OnGUI()
{
	if(DisplayYouAreHere)
   	{
   		GUI.Label (Rect (areHereX,areHereY, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(DisplayMissionEnd)
   	{
   		GUI.Label (Rect (endX,endY, 29,29), missionEnd, GUIStyle.none);	
   	}
   	if(DisplayBeenHere)
   	{
   		GUI.Label (Rect (beenHereX,beenHereY, 29,29), beenHereBefore, GUIStyle.none);	
   	}
   	
}

Translate your character’s x/z coordinates to your map’s x/z coordinates.

var mapSize : Vector2;
var mapOrigin : Vector2;
var worldSize : Vector2; // X/Z plane only
var worldOrigin : Vector2; // XZ coordinates corresponding to MapOrigin

private var worldToMapRatio : Vector2;

function Start() {
  worldToMapRatio = new Vector2( mapSize.x / worldSize.x, mapSize.y / worldSize.y );
}  

function CalculatePlayerPositionOnMap() : Vector2 {
  var pos : Vector2 = ( worldToMapRatio.x * (playerTransform.x + worldOrigin.x), worldToMapRatio.y * (playerTransform.y + worldOrigin.y) );
  pos += mapOrigin;
  return pos;
}

I can’t run Unity to test that out right now, but that should be pretty close. Might need to subtract world origin or not add map origin; I can be bad at math sometimes. Essentially you ignore the Y axis of the 3D world and scale it to fit on the map; then your player’s scaled position is where you want the indicator to go.

Wow Vicenti thats mind blowing lol. Im going to have to stair at this one for a while and how would I connect the pieces like the 2d texture of the youAreHere variable?

I seriously appreciate the post and will definitely try it! THANK YOU!

var mapSize : Vector2;
var worldSize : Vector2; // XZ
var worldOrigin : Vector2; // lowest X/Z coordinates of world
var mapOrigin : Vector2; // pixel position of map
var mapTexture : Texture2D;
var playerIndicatorTexture : Texture2D;
var playerIndicatorOffset : Vector2; // offset player texture by this much - for texs that surround the point rather than point at it

private var playerTransform : Transform;
private var worldToMapRatio : Vector2;

function Start() {
  worldToMapRatio = new Vector2( mapSize.x / worldSize.x, mapSize.y / worldSize.y );
  playerTransform = GameObject.FindObjectWithTag( "Player" ).transform; // alter this to somehow acquire the player transform, or make it not a private var and just assign it yourself if that's more better
}

function TranslateWorldToMap( pos : Vector3 ) : Vector2 {
  var out : Vector2 = new Vector2( (pos.x - worldOrigin.x) * worldToMapRatio.x, (pos.z - worldOrigin.y) * worldToMapRatio.y );
  out+= mapOrigin;
  return out;
}

function OnGUI() {
  DrawTexture( mapTexture, mapOrigin );
  DrawTexture( playerIndicatorTexture, playerIndicatorOffset + TranslateWorldToMap( playerTransform.position ) );
}

function DrawTexture( t : Texture2D, p : Vector2 ) {
  GUI.Label( Rect( p.x, p.y, t.width, t.height ), t, GUIStyle.none );
}

Again, I’m unable to test right now, but that might be of more assistance. :slight_smile: It should also be easily extendible to include for example indicators for enemies or pickups.

Hey Vicenti YOU ARE A GENIUS! I dont know how you could ever figure something like this out :slight_smile: I love it and it works great :smile:
Im still playing around with it because when I go up the indicator goes down and vice versa but I’ll figure it out you’ve done enough and Im very appreciative! I also figured out how to place the pickup icons and toying with the enemies and will post my code for this as soon as I get it down patt which should be very soon. Im still starring at it until I can completely understand it lol.

But Thanks a million my friend.

Glad to be of service! Sorry my math can be a little funky sometimes. I blame Windows for allowing two different coordinate systems to coexist. :slight_smile:

You might try changing the line in WorldToMap to

  var out : Vector2 = new Vector2( (pos.x - worldOrigin.x) * worldToMapRatio.x, Screen.height - (pos.z - worldOrigin.y) * worldToMapRatio.y );

Or

  var out : Vector2 = new Vector2( (pos.x - worldOrigin.x) * worldToMapRatio.x, (worldOrigin.y - pos.z) * worldToMapRatio.y );

Sounds like I forgot about GUI coordinates versus ‘correct’ coordinates again. Eyeroll. :slight_smile:

The first one works like a charm :smile: I cant wait to show off a demo :smile: and your going into the credits my friend!