I'm trying to create a radar effect (almost GTA-shaped minimap). The radar shows the position of an enemy (represented by a small image) when in range.
The problem I have here is: I want the radar to indicate if the enemy is higher or lower than the player (if the y-position of the enemy is greater or less than the y-position of the player).
I want to accomplish this goal by switching the image on the radar (representing the enemy) to a different image relative to the y-position of the enemy.
Here is my radar script:
public class RadarObject
{
public Image icon { get; set; }
public GameObject owner { get; set; }
}
public radar: MonoBehaviour
{
(SerializeField) private Transform playerPosition;
private float MapScale = 2.0f;
private Image NormalImage;
private Image UpImage;
private Image DownImage;
public static List radObjects = new List();
public static Radar Instance;
private void Awake()
{
Instance = this;
}
public void CustomRegisterRadarObject(GameObject o, Image u, Image d, Image n)
{
NormalImage = Instantiate(n);
UpImage = Instantiate(u);
DownImage = Instantiate(d);
radObjects.Add(new RadarObject() { owner = o, icon = NormalImage });
radObjects.Add(new RadarObject() { owner = o, icon = UpImage });
radObjects.Add(new RadarObject() { owner = o, icon = DownImage });
}
public static void RemoveRadarObject(GameObject o)
{
List newList = new List();
for(int i = 0; i < radObjects.Count; i++)
{
if(radObjects(i).owner == o)
{
Destroy(radObjects(i).icon);
continue;
}
else
{
newList.Add(radObjects(i));
}
}
radObjects.RemoveRange(0, radObjects.Count);
radObjects.AddRange(newList);
}
private void DrawRadarDots()
{
foreach (RadarObject ro in radObjects)
{
Vector3 radarPos = (ro.owner.transform.position - playerPosition.position);
float distToObject = Vector3.Distance(playerPosition.position, ro.owner.transform.position) * MapScale;
float deltaY = Mathf.Atan2(radarPos.x, radarPos.z) * Mathf.Rad2Deg - 270 - playerPosition.eulerAngles.y;
radarPos.x = distToObject * Mathf.Cos(deltaY * Mathf.Deg2Rad) * -1;
radarPos.z = distToObject * Mathf.Sin(deltaY * Mathf.Deg2Rad);
ro.icon.transform.SetParent(transform);
ro.icon.transform.position = new Vector3(radarPos.x, radarPos.z, 0) + transform.position;
}
}
void Update()
{
DrawRadarDots();
}
public void CheckHeightDifference(Transform o)
{
foreach (RadarObject ro in radObjects)
{
if (o.position.y > playerPosition.position.y && ro.icon.color != Color.yellow) //if enemy is above the player
{
//ro.icon.color = Color.yellow;
ro.icon = UpImage;
}
else if (o.position.y < playerPosition.position.y && ro.icon.color != Color.red) //if enemy is below the player
{
//ro.icon.color = Color.red;
ro.icon = DownImage;
}
else if (o.position.y == playerPosition.position.y && ro.icon.color != Color.green) //if enemy and the player are at the same height
{
//ro.icon.color = Color.green;
ro.icon = NormalImage;
}
}
}
}
And here's the script attached to the enemy to make it visible on the radar:
public class MakeAvailableOnRadar : MonoBehaviour
{
(SerializeField) private Image RadarImage; //Image that shows if the player and enemy are at the same height
(SerializeField) private Image RadarImageUp; //Image that shows if the enemy is higher than the player
(SerializeField) private Image RadarImageDown; //Image that shows if the enemy is lower than the player
private Radar radar;
void Start()
{
radar = Radar.Instance;
radar.CustomRegisterRadarObject(gameObject, RadarImageUp, RadarImageDown, RadarImage);
}
private void Update()
{
radar.CheckHeightDifference(transform);
}
void OnDisable()
{
Radar.RemoveRadarObject(gameObject);
}
}
It does not seem to work the way I want it to. Is there a workaround? How do I fix this?
I follow Hollistic3D's Creating a Mini Card System in Unity 5 (https://www.youtube.com/watch?v=LP2XHHeQuvg) as a guide.