- Home /
 
Minimap enemy icon help?
Let's say I was trying to make a minimap. For my case, my minimap is an image with a circular mask. The image is a raw image that takes in a texture that my minimap camera is rendering.
I have an enemy AI in my game that moves around right? He should have an enemy icon.
What I did was I made that enemy take in a script called MinimapItem. Here's the code
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class MinimapItem : MonoBehaviour
 {
     [Tooltip("The prefab that will be used to instantiate an icon into the map")]
     public GameObject IconPrefab;
 
     [Tooltip("The parent that the instantiated icon should be parented to")]
     public RectTransform MinimapParent;
 
     [Tooltip("The sprite of the icon")]
     public Sprite iconSprite;
 
     [Tooltip("The death sprite for the icon")]
     public Sprite deathSprite;
 
     [Tooltip("The target to follow")]
     public Transform Target;
     
     public void CreateIcon()
     {
         GameObject newIcon = Instantiate(IconPrefab) as GameObject;
 
         newIcon.transform.SetParent(MinimapParent, false);
 
         if (iconSprite != null)        
             newIcon.GetComponent<MinimapIcon>().aliveIcon = iconSprite;
 
         if (deathSprite != null)
             newIcon.GetComponent<MinimapIcon>().deadIcon = deathSprite;
 
         newIcon.GetComponent<MinimapIcon>().Target = Target;
         newIcon.GetComponent<MinimapIcon>().RectRoot = MinimapParent;
 
         newIcon.GetComponent<MinimapIcon>().Init();
 
         
         
     }
   
 
 }
 
 
               The icon that is instantiated has a script called MinimapIcon. Here's the code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using Invector;
 using Invector.CharacterController;
 using Invector.EventSystems;
 
 public class MinimapIcon : MonoBehaviour {
 
     [HideInInspector]
     public Sprite aliveIcon;
 
     [HideInInspector]
     public Sprite deadIcon;
 
     [HideInInspector]
     public Transform Target;
 
     [HideInInspector]
     public RectTransform RectRoot;
 
     public void Init()
     {
         if (aliveIcon == null)
             print("no alive icon present");
 
         this.GetComponent<Image>().sprite = aliveIcon;
         //Have OnKilled listen to the death event of the AI
    
     }
 
     public void FixedUpdate()
     {
         Vector2 viewPortPos = GameObject.FindGameObjectWithTag("MapCamera").GetComponent<Camera>().WorldToViewportPoint(Target.position);
 
         print("viewPortPos" + viewPortPos);
 
         GetComponent<RectTransform>().position = viewPortPos;
     }
 
     public void OnKilled()
     {
         this.GetComponent<Image>().sprite = deadIcon;
     }
 
 }
 
 
               Notice how the icon gets instantiated into the rect transform of my minimap raw image i was talking about in the beginning.
My goal is to make the icon follow the enemy AI, but in like a certain scale with the UI the main camera sees (let me know if I need to be more clear, its hard to explain. If you look at UGUI Minimap's code, it's the same idea). I was thinking maybe it had to do with WorldToViewportPoint but thats not working. Ideas?
Your answer