Help with my radar please.

Hey there everyone, I know there is a radar script on the unity wiki that I could use, but I want to make my own and I am so close to completing it. Here is the code(that you may want to copy to a text editor(lol)):

using UnityEngine;
using System.Collections;

[AddComponentMenu("HUD/Radar")]
public class Radar : MonoBehaviour {
	//Radar texture/positioning crap
	public Texture2D radarImage;
	public Texture2D ourBlip;
	public Texture2D enemyBlip;
	public Vector2 radarSize;
	public Vector2 blipSize;
	
	//The rest
	public Transform pivOffset;
	private GameObject[] enemies;
	public float maxDistance;
	
	void OnGUI()
	{
		enemies = GameObject.FindGameObjectsWithTag("Finish");	    
		Vector2 radCenter = new Vector2(Screen.width - (radarSize.x / 2), Screen.height - (radarSize.y / 2));
		Vector2 radarPlace = new Vector2(Screen.width - radarSize.x, Screen.height - radarSize.y);
		GUI.DrawTexture(new Rect(radarPlace.x, radarPlace.y, radarSize.x, radarSize.y), radarImage, ScaleMode.ScaleToFit, true);
		GUI.DrawTexture(new Rect(radCenter.x - (blipSize.x / 2), radCenter.y - (blipSize.y / 2), blipSize.x, blipSize.y), ourBlip, ScaleMode.ScaleToFit, true);
		foreach(GameObject enemy in enemies)
		{
			float distance;			
			distance = Vector3.Distance(enemy.transform.position, pivOffset.position);
			if(distance <= maxDistance)
			{
				Vector2 position2D = new Vector2(enemy.transform.position.x - transform.position.x, enemy.transform.position.z - transform.position.z);
				DrawBlips(distance, enemyBlip, radCenter, position2D);
			}
		}
	}
	
	void DrawBlips(float distance, Texture2D blip, Vector2 offset, Vector2 pos2D)
	{
		float scaleRadarRange = maxDistance / radarSize.y;
		pos2D.x /= scaleRadarRange;
		pos2D.x /= 2;
		pos2D.y /= scaleRadarRange;
		pos2D.y /= 2;
		GUIUtility.RotateAroundPivot(-pivOffset.eulerAngles.y, offset);		
		GUI.DrawTexture(new Rect(offset.x - (blipSize.x / 2) + pos2D.x, (offset.y - (blipSize.y /2)) - pos2D.y, blipSize.x, blipSize.y), blip);
	}
}

It works great when there is only one enemy within distance. But if there are two or more, then GUIUtility.RotateAroundPivot, stacks for each enemy. Any suggestions on how to fix this?

Cough

Reset the gui matrix after you use it.

–Eric

I’m sorry, I tried to figure it out, but how would one go about resetting the matrix?

Store it in a variable before altering it and then load it back again.

–Eric

Thats how I was going to do it, I just don’t know which variable to modify.

Heh, oops. I figured it out. It was simpler than I thought, too simple.

Here are the end results for anyone wanting a different radar.

using UnityEngine;
using System.Collections;

[AddComponentMenu("HUD/Radar")]
public class Radar : MonoBehaviour {
	//Radar texture/positioning crap
	public Texture2D radarImage;
	public Texture2D ourBlip;
	public Texture2D enemyBlip;
	public Vector2 radarSize;
	public Vector2 blipSize;
	
	//The rest
	public Transform pivOffset;
	private GameObject[] enemies;
	public float maxDistance;
	
	void OnGUI()
	{
		enemies = GameObject.FindGameObjectsWithTag("Finish");	    
		Vector2 radCenter = new Vector2(Screen.width - (radarSize.x / 2), Screen.height - (radarSize.y / 2));
		Vector2 radarPlace = new Vector2(Screen.width - radarSize.x, Screen.height - radarSize.y);
		GUI.DrawTexture(new Rect(radarPlace.x, radarPlace.y, radarSize.x, radarSize.y), radarImage, ScaleMode.ScaleToFit, true);
		GUI.DrawTexture(new Rect(radCenter.x - (blipSize.x / 2), radCenter.y - (blipSize.y / 2), blipSize.x, blipSize.y), ourBlip, ScaleMode.ScaleToFit, true);
		foreach(GameObject enemy in enemies)
		{
			float distance;			
			distance = Vector3.Distance(enemy.transform.position, pivOffset.position);
			if(distance <= maxDistance)
			{
				Vector2 position2D = new Vector2(enemy.transform.position.x - transform.position.x, enemy.transform.position.z - transform.position.z);
				DrawBlips(distance, enemyBlip, radCenter, position2D);
			}
		}
	}
	
	void DrawBlips(float distance, Texture2D blip, Vector2 offset, Vector2 pos2D)
	{
		float scaleRadarRange = maxDistance / radarSize.y;
		pos2D.x /= scaleRadarRange;
		pos2D.x /= 2;
		pos2D.y /= scaleRadarRange;
		pos2D.y /= 2;
        Matrix4x4 stuff = GUI.matrix;
		GUIUtility.RotateAroundPivot(-pivOffset.eulerAngles.y, offset);		
		GUI.DrawTexture(new Rect(offset.x - (blipSize.x / 2) + pos2D.x, (offset.y - (blipSize.y /2)) - pos2D.y, blipSize.x, blipSize.y), blip);
		GUI.matrix = stuff;
	}
}