Hi all! I’ve been working to create a nice little circular minimap in unity basic, and while it’s almost complete, I’m just having a bit of trouble on the blips. What I have done is created an Orthographic camera from above looking down at the player. It’s Culling mask is set to “Map”, which the camera can see a 2D map of the level, and the Normalized View Port is changed to sit the minimap at the bottom right.
The following script follows the player and rotates along with my orbit camera, as well as setting up a border:
var player : Transform;
var mainCamera : Transform;
var mapBorder : Texture2D;
private var showMap = true;
function Start(){
if(showMap){
this.GetComponent(Camera).enabled = true;
}
if(!showMap){
this.GetComponent(Camera).enabled = false;
}
}
function FixedUpdate(){
transform.position = new Vector3(player.position.x, transform.position.y, player.position.z);
transform.eulerAngles.y = mainCamera.eulerAngles.y;
}
function MapOn(){
showMap = true;
}
function MapOff(){
showMap = false;
}
function OnGUI(){
if(showMap){
GUI.DrawTexture(new Rect(Screen.width * 0.713f,Screen.height * 0.607f,Screen.width/3, Screen.height/2.7), mapBorder);
camera.Render();
}
}
I would like to know if there is some way to visually define other objects in my minimap by tags. For instance, if I have a tag called “Enemy”, how can the blip texture for the enemy appear, rotate with the minimap, stay within the minimap and represent the enemy? I’d also like to know how could I make a map icon sit on the border of the map when the player goes too far from it, like in GTA.
Okay, so what I did was that I created an Enemy Blip and set it on a plane. I made the plane’s Layer Mask “Map” and a child of the enemy, so that the minimap camera can only see the blip and so the enemy carries it. This is something I’m just settling for since it’s difficult for me to figure this out. But if this is the best way, could someone also help me on how to restrict some “blips” from leaving the minimap and lock on the border when the player is too far?
Thank you!
var enemyBlip : Texture2D;
var centerObject : Transform;
var distance : float = 0.1;
var mapScale : float = 0.1;
var mapCenter : Vector2; //x=1145, y= 525
function OnGUI(){
DrawBlipsForEnemies();
}
function DrawBlipsForEnemies(){
// Find all game objects tagged Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and call drawBlip function
for (var go : GameObject in gos) {
drawBlip(go,enemyBlip);
}
}
function drawBlip(go,aTexture){
centerPos=centerObject.position;
extPos=go.transform.position;
// first we need to get the distance of the enemy from the player
dist=Vector3.Distance(centerPos,extPos);
dx=centerPos.x-extPos.x; // how far to the side of the player is the enemy?
dz=centerPos.z-extPos.z; // how far in front or behind the player is the enemy?
// what's the angle to turn to face the enemy - compensating for the player's turning?
deltay=Mathf.Atan2(dx,dz)*Mathf.Rad2Deg - 270 - centerObject.eulerAngles.y;
// just basic trigonometry to find the point x,y (enemy's location) given the angle deltay
bX=dist*Mathf.Cos(deltay * Mathf.Deg2Rad);
bY=dist*Mathf.Sin(deltay * Mathf.Deg2Rad);
if(dist>=distance){
bX=bX*mapScale; // scales down the x-coordinate so that the plot stays within our radar
bY=bY*mapScale; // scales down the y-coordinate so that the plot stays within our radar
// this is the diameter of our largest radar circle
GUI.DrawTexture(Rect(mapCenter.x+bX,mapCenter.y,27,25),aTexture);
}
}
It does draw blips on the Map, but it’s kinda screwy. The center object is my orbit camera so all the blips on the map won’t turn when the player turns, but turn along with the map. The player which is the center of the map does not seem to get closer to the enemy as I travel. It’s just a matter of how can I make the blip stay on top of the tag “Enemy” object without moving around everywhere?
Well, toying with the above all day isn’t getting anywhere, so now I’m back at square one: Parenting planes with blip images that only the minimap camera can see. This really isn’t a bad idea. The only thing is that when a specific blip is out of view, how can I make that blip/plane dock itself on the edge of the the minimap? Can anyone help? This seems almost impossible!
Tackling this one step at a time I figured out how to create a North Blip. I used the layer method to create a blip image on a plane and set it at a certain distance from the player so that it looks as if the blip is near the edge of my mini map. I used the same line of code that the second camera uses to follow the player without inheriting his rotations. Here’s the full script so far if anyone’s interested in using it, works best if the main camera is an orbit camera. Just put it on an orthographic camera looking down at the player:
@script ExecuteInEditMode()
var player : Transform;
var mainCamera : Transform;
var mapBorder : Texture2D;
var northBlip : Transform;
var distance : float = 100;
function FixedUpdate(){
transform.position = new Vector3(player.position.x, transform.position.y, player.position.z);
transform.eulerAngles.y = mainCamera.eulerAngles.y;
northBlip.transform.position = new Vector3(player.position.x, player.position.y + 500, player.position.z + distance);
}
function OnGUI(){
if(showMap){
GUI.DrawTexture(new Rect(Screen.width * 0.713f,Screen.height * 0.607f,Screen.width/3, Screen.height/2.7), mapBorder);
camera.Render();
}
}
Here’s what my minimap looks like:
I’m still trying to figure out how I can dock image planes to the minimap’s border when the player is far at a certain distance. If anyone has any answers, I’d greatly appreciate it!