Author Topic: Can someone convert this radar script for NGUI?  (Read 2439 times)

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
Can someone convert this radar script for NGUI?
« on: November 12, 2012, 10:51:06 AM »
Hi,

Just wondering if someone here can convert this radar script so it can be used with NGUI please?

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Radar : MonoBehaviour
  5. {
  6.  
  7. // radar! by oPless from the original javascript by PsychicParrot,
  8. // who in turn adapted it from a Blitz3d script found in the
  9. // public domain online somewhere ....
  10. //
  11.  
  12. public Texture blip  ;
  13. public Texture radarBG ;
  14.  
  15.         public Transform centerObject ;
  16.         public float mapScale = 0.3f;
  17.         public Vector2 mapCenter = new Vector2(50,50);
  18.         public string tagFilter =  "enemy";
  19.         public float maxDist = 200;
  20.  
  21.         void OnGUI()
  22.         {
  23.  
  24.                 //      GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3(Screen.width / 600.0, Screen.height / 450.0, 1));
  25.  
  26.                 // Draw player blip (centerObject)
  27. //              float bX=centerObject.transform.position.x * mapScale;
  28. //          float bY=centerObject.transform.position.z * mapScale;
  29.  
  30.  
  31.                 GUI.DrawTexture(new Rect(mapCenter.x-32,mapCenter.y-32,64,64),radarBG);
  32.                 DrawBlipsFor(tagFilter);
  33.  
  34.         }
  35.  
  36.         private void DrawBlipsFor(string tagName)
  37.         {
  38.  
  39.                  // Find all game objects with tag
  40.             GameObject[] gos = GameObject.FindGameObjectsWithTag(tagName);
  41.  
  42.             // Iterate through them
  43.             foreach (GameObject go in gos)  
  44.             {
  45.                         drawBlip(go,blip);
  46.             }
  47.         }
  48.  
  49.         private void drawBlip(GameObject go,Texture aTexture)
  50.         {
  51.                 Vector3 centerPos=centerObject.position;
  52.                 Vector3 extPos=go.transform.position;
  53.  
  54.                 // first we need to get the distance of the enemy from the player
  55.                 float dist=Vector3.Distance(centerPos,extPos);
  56.  
  57.                 float dx=centerPos.x-extPos.x; // how far to the side of the player is the enemy?
  58.                 float dz=centerPos.z-extPos.z; // how far in front or behind the player is the enemy?
  59.  
  60.                 // what's the angle to turn to face the enemy - compensating for the player's turning?
  61.                 float deltay=Mathf.Atan2(dx,dz)*Mathf.Rad2Deg - 270 - centerObject.eulerAngles.y;
  62.  
  63.                 // just basic trigonometry to find the point x,y (enemy's location) given the angle deltay
  64.                 float bX=dist*Mathf.Cos(deltay * Mathf.Deg2Rad);
  65.                 float bY=dist*Mathf.Sin(deltay * Mathf.Deg2Rad);
  66.  
  67.                 bX=bX*mapScale; // scales down the x-coordinate by half so that the plot stays within our radar
  68.                 bY=bY*mapScale; // scales down the y-coordinate by half so that the plot stays within our radar
  69.  
  70.                 if(dist<= maxDist)
  71.                 {
  72.                         // this is the diameter of our largest radar circle
  73.                    GUI.DrawTexture(new Rect(mapCenter.x+bX,mapCenter.y+bY,2,2),aTexture);
  74.                 }
  75.  
  76.         }
  77.  
  78.  
  79. }