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?