Radar

From Unify Community Wiki
Revision as of 17:54, 12 August 2009 by DastardlyBanana (Talk | contribs)

Jump to: navigation, search

Contents

Radar Script

Author: PsychicParrot

After seeing requests for radars quite frequently in the forums, I decided to post this up for everyone to enjoy and hopefully build on :)

Don't know if it's the most efficient way to do this or whatever, but it works and it seems to work pretty well!

USAGE:

Put this script on a GameObject. Select a texture for a 'blip'. Select a texture for the radar background (radarBG) Set the map center (on screen coordinates) Set the GameObject to use as your center object (usually the player) Make sure that all enemies are tagged with 'Enemy' Watch your radar goodness in your game ;)

I included the GUI Matrix scaling line just incase ... if you want it to auto-scale (which is a good thing to do!) un-comment it and change the values to your own game screen resolution.

Enjoy! Parrot.

JavaScript

<javascript>

@script ExecuteInEditMode()

// radar! by PsychicParrot, adapted from a Blitz3d script found in the public domain online somewhere .. //

public var blip : Texture; public var radarBG : Texture;

public var centerObject : Transform; public var mapScale = 0.3; public var mapCenter = Vector2(50,50);

function OnGUI () {

// GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3(Screen.width / 600.0, Screen.height / 450.0, 1));

// Draw player blip (centerObject) bX=centerObject.transform.position.x * mapScale;

   bY=centerObject.transform.position.z * mapScale;
   	
   bX=centerObject.transform.position.x * mapScale;
   bY=centerObject.transform.position.z * mapScale;
   	
	GUI.DrawTexture(Rect(mapCenter.x-32,mapCenter.y-32,64,64),radarBG);

// Draw blips for zombies DrawBlipsForEnemies();

}

function DrawBlipsForCows(){

// Find all game objects with tag Cow

   var gos : GameObject[];
   gos = GameObject.FindGameObjectsWithTag("Cow"); 
   var distance = Mathf.Infinity; 
   var position = transform.position; 
   // Iterate through them
   for (var go : GameObject in gos)  { 

drawBlip(go,blip2);

   }

}

function drawBlip(go,aTexture){

centerPos=centerObject.position; extPos=go.transform.position;

// first we need to get the distance of the enemy from the player dist=Vector3.Distance(centerPos,extPos);

dx=centerPos.x-extPos.x; // how far to the side of the player is the enemy? dz=centerPos.z-extPos.z; // how far in front or behind the player is the enemy?

// what's the angle to turn to face the enemy - compensating for the player's turning? deltay=Mathf.Atan2(dx,dz)*Mathf.Rad2Deg - 270 - centerObject.eulerAngles.y;

// just basic trigonometry to find the point x,y (enemy's location) given the angle deltay bX=dist*Mathf.Cos(deltay * Mathf.Deg2Rad); bY=dist*Mathf.Sin(deltay * Mathf.Deg2Rad);

bX=bX*mapScale; // scales down the x-coordinate by half so that the plot stays within our radar bY=bY*mapScale; // scales down the y-coordinate by half so that the plot stays within our radar

if(dist<=200){ // this is the diameter of our largest radar circle GUI.DrawTexture(Rect(mapCenter.x+bX,mapCenter.y+bY,2,2),aTexture);

}

}

function DrawBlipsForEnemies(){

   // Find all game objects tagged Enemy
   var gos : GameObject[];
   gos = GameObject.FindGameObjectsWithTag("Enemy"); 
   var distance = Mathf.Infinity; 
   var position = transform.position; 
   // Iterate through them and call drawBlip function
   for (var go : GameObject in gos)  { 

drawBlip(go,blip);

   }

}


</javascript>


CSharp Version

Author: oPless A C# version of the script above (slightly modified)

<csharp> using UnityEngine; using System.Collections;

public class Radar : MonoBehaviour {

// radar! by oPless from the original javascript by PsychicParrot, // who in turn adapted it from a Blitz3d script found in the // public domain online somewhere .... //

public Texture blip  ; public Texture radarBG ;

public Transform centerObject ; public float mapScale = 0.3f; public Vector2 mapCenter = new Vector2(50,50); public string tagFilter = "enemy"; public float maxDist = 200;

void OnGUI() {

// GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3(Screen.width / 600.0, Screen.height / 450.0, 1));

// Draw player blip (centerObject) // float bX=centerObject.transform.position.x * mapScale; // float bY=centerObject.transform.position.z * mapScale;


GUI.DrawTexture(new Rect(mapCenter.x-32,mapCenter.y-32,64,64),radarBG); DrawBlipsFor(tagFilter);

}

private void DrawBlipsFor(string tagName) {

// Find all game objects with tag GameObject[] gos = GameObject.FindGameObjectsWithTag(tagName);

// Iterate through them foreach (GameObject go in gos) { drawBlip(go,blip); } }

private void drawBlip(GameObject go,Texture aTexture) { Vector3 centerPos=centerObject.position; Vector3 extPos=go.transform.position;

// first we need to get the distance of the enemy from the player float dist=Vector3.Distance(centerPos,extPos);

float dx=centerPos.x-extPos.x; // how far to the side of the player is the enemy? float dz=centerPos.z-extPos.z; // how far in front or behind the player is the enemy?

// what's the angle to turn to face the enemy - compensating for the player's turning? float deltay=Mathf.Atan2(dx,dz)*Mathf.Rad2Deg - 270 - centerObject.eulerAngles.y;

// just basic trigonometry to find the point x,y (enemy's location) given the angle deltay float bX=dist*Mathf.Cos(deltay * Mathf.Deg2Rad); float bY=dist*Mathf.Sin(deltay * Mathf.Deg2Rad);

bX=bX*mapScale; // scales down the x-coordinate by half so that the plot stays within our radar bY=bY*mapScale; // scales down the y-coordinate by half so that the plot stays within our radar

if(dist<= maxDist) { // this is the diameter of our largest radar circle GUI.DrawTexture(new Rect(mapCenter.x+bX,mapCenter.y+bY,2,2),aTexture); }

}


}

</csharp>


Modified to allow many radar locations based on the CSharp Version

Author: Zumwalt

<csharp>

 public class CLSRadar
 {
   // expanded radar! by zumwalt, alteration of oPress's version which is
   // from the original javascript by PsychicParrot, 
   // who in turn adapted it from a Blitz3d script found in the
   // public domain online somewhere ....
   //
   public Texture blip;
   public Texture radarBG;
   public Transform centerObject;
   public float mapScale = 0.3f;
   public Vector2 mapCenter;
   public float screenWidth=1024.0f;
   public float screenHeight=768.0f;
   public int mapLocation = 0;
   // 0 = upper left
   // 1 = upper right
   // 2 = lower left
   // 3 = lower right
   public string tagFilter = "enemy";
   public float maxDist = 200;
   /// <summary>
   /// Switch DrawRadar() to OnGui if you use this directly in a unity C# Script object outside of this class
   /// otherwise, within the OnGui() call DrawRadar() on the class object.
   /// </summary>
   public void DrawRadar()
   {
     //UnityEngine.GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(Screen.width / 600.0f, Screen.height / 450.0f, 1.0f));
     // Draw player blip (centerObject)
     switch (mapLocation)
     {
       case 0:
         UnityEngine.GUI.DrawTexture(new Rect(0, 0, radarBG.width,radarBG.height), radarBG);
         mapCenter = new Vector2(radarBG.width / 2, radarBG.height / 2);
         break;
       case 1:
         UnityEngine.GUI.DrawTexture(new Rect(screenWidth - radarBG.width, 0, radarBG.width, radarBG.height), radarBG);
         mapCenter = new Vector2(screenWidth-(radarBG.width / 2), radarBG.height / 2);
         break;
       case 2:
         UnityEngine.GUI.DrawTexture(new Rect(0,screenHeight-radarBG.height, radarBG.width, radarBG.height), radarBG);
         mapCenter = new Vector2(radarBG.width / 2, screenHeight-(radarBG.height / 2));
         break;
       case 3:
         UnityEngine.GUI.DrawTexture(new Rect(screenWidth - radarBG.width, screenHeight - radarBG.height, radarBG.width, radarBG.height), radarBG);
         mapCenter = new Vector2(screenWidth-(radarBG.width / 2),screenHeight-(radarBG.height / 2));
         break;
     }
     
     DrawBlipsFor(tagFilter);
   }
   private void DrawBlipsFor(string tagName)
   {
     // Find all game objects with tag 
     GameObject[] gos = GameObject.FindGameObjectsWithTag(tagName);
     // Iterate through them
     foreach (GameObject go in gos)
     {
       drawBlip(go, blip);
     }
   }
   private void drawBlip(GameObject go, Texture aTexture)
   {
     Vector3 centerPos = centerObject.position;
     Vector3 extPos = go.transform.position;
     // first we need to get the distance of the enemy from the player
     float dist = Vector3.Distance(centerPos, extPos);
     float dx = centerPos.x - extPos.x; // how far to the side of the player is the enemy?
     float dz = centerPos.z - extPos.z; // how far in front or behind the player is the enemy?
     // what's the angle to turn to face the enemy - compensating for the player's turning?
     float deltay = Mathf.Atan2(dx, dz) * Mathf.Rad2Deg - 270 - centerObject.eulerAngles.y;
     // just basic trigonometry to find the point x,y (enemy's location) given the angle deltay
     float bX = dist * Mathf.Cos(deltay * Mathf.Deg2Rad);
     float bY = dist * Mathf.Sin(deltay * Mathf.Deg2Rad);
     bX = bX * mapScale; // scales down the x-coordinate by half so that the plot stays within our radar
     bY = bY * mapScale; // scales down the y-coordinate by half so that the plot stays within our radar
     if (dist <= maxDist)
     {
       // this is the diameter of our largest radar circle
       UnityEngine.GUI.DrawTexture(new Rect(mapCenter.x + bX, mapCenter.y + bY, 2, 2), aTexture);
     }
   }
 }

</csharp>

Updated JavaScript Version

Author: DastardlyBanana

This update makes the radar location more flexible, since you can easily define its location in the inspector using one of 9 preset locations or by defining your own. We have also made the radar interact with the enemies it tracks, seeing whether or not they are aware of the player and displaying aware enemies in a different color (this is dependent on the AI script of the enemies- in our game the enemies turn red when chasing the player, but this could be changed to fit the needs of the game it is in). It is also easily possible to change the size of the radar now, based on a percentage of the screen ( so it scales with the screen).

A unity package containing the radar script, demo scene and example script for interaction with enemy AI can be found at our website: [[1]]

<JavaScript> @script ExecuteInEditMode() // radar! by PsychicParrot, adapted from a Blitz3d script found in the public domain online somewhere ..

//Modified by Dastardly Banana to add radar size configuration, different colors for enemies in different states (patrolling or chasing), ability to move radar to either one of 9 preset locations or to custom location.

//some lines are particular to our AI script, you will need to change "EnemyAINew" to the name of your AI script, and change "isChasing" to the boolean within that AI script that is true when the enemy is active/can see the player/is chasing the player.

var blip : Texture; // texture to use when the enemy isn't chasing var blipChasing : Texture; //When Chasing var radarBG : Texture;

var centerObject : Transform; var mapScale = 0.3; var mapSizePercent = 15;

var checkAIscript : boolean = true; var enemyTag = "Enemy";

enum radarLocationValues {topLeft, topCenter, topRight, middleLeft, middleCenter, middleRight, bottomLeft, bottomCenter, bottomRight, custom} var radarLocation : radarLocationValues = radarLocationValues.bottomLeft;

private var mapWidth : float; private var mapHeight : float; private var mapCenter : Vector2; var mapCenterCustom : Vector2;

function Start () { setMapLocation(); }

function OnGUI () { // GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3(Screen.width / 600.0, Screen.height / 450.0, 1));

	// Draw player blip (centerObject)
	bX=centerObject.transform.position.x * mapScale;
	bY=centerObject.transform.position.z * mapScale;	
	GUI.DrawTexture(Rect(mapCenter.x - mapWidth/2,mapCenter.y-mapHeight/2,mapWidth,mapHeight),radarBG);

// Draw blips for Enemies DrawBlipsForEnemies();

}

function drawBlip(go,aTexture){

centerPos=centerObject.position; extPos=go.transform.position;

// first we need to get the distance of the enemy from the player dist=Vector3.Distance(centerPos,extPos);

dx=centerPos.x-extPos.x; // how far to the side of the player is the enemy? dz=centerPos.z-extPos.z; // how far in front or behind the player is the enemy?

// what's the angle to turn to face the enemy - compensating for the player's turning? deltay=Mathf.Atan2(dx,dz)*Mathf.Rad2Deg - 270 - centerObject.eulerAngles.y;

// just basic trigonometry to find the point x,y (enemy's location) given the angle deltay bX=dist*Mathf.Cos(deltay * Mathf.Deg2Rad); bY=dist*Mathf.Sin(deltay * Mathf.Deg2Rad);

bX=bX*mapScale; // scales down the x-coordinate so that the plot stays within our radar bY=bY*mapScale; // scales down the y-coordinate so that the plot stays within our radar

if(dist<=mapWidth*.5/mapScale){ // this is the diameter of our largest radar circle GUI.DrawTexture(Rect(mapCenter.x+bX,mapCenter.y+bY,4,4),aTexture);

}

}

function DrawBlipsForEnemies(){ //You will need to replace isChasing with a variable from your AI script that is true when the enemy is chasing the player, or doing watever you want it to be doing when it is red on the radar.

//You will need to replace "EnemyAINew with the name of your AI script

   // Find all game objects tagged Enemy
   var gos : GameObject[];
   gos = GameObject.FindGameObjectsWithTag(enemyTag); 

   var distance = Mathf.Infinity; 
   var position = transform.position; 

   // Iterate through them and call drawBlip function
   for (var go : GameObject in gos)  { 
  		 	var blipChoice : Texture = blip;
  		   	if(checkAIscript){
   			var aiScript : EnemyAI = go.GetComponent("EnemyAI");
   		if(aiScript.isChasing)
   				blipChoice = blipChasing;
   	}

drawBlip(go,blipChoice);

   }

}

function setMapLocation () { mapWidth = Screen.width*mapSizePercent/100.0; mapHeight = mapWidth;

//sets mapCenter based on enum selection if(radarLocation == radarLocationValues.topLeft){ mapCenter = Vector2(mapWidth/2, mapHeight/2); } else if(radarLocation == radarLocationValues.topCenter){ mapCenter = Vector2(Screen.width/2, mapHeight/2); } else if(radarLocation == radarLocationValues.topRight){ mapCenter = Vector2(Screen.width-mapWidth/2, mapHeight/2); } else if(radarLocation == radarLocationValues.middleLeft){ mapCenter = Vector2(mapWidth/2, Screen.height/2); } else if(radarLocation == radarLocationValues.middleCenter){ mapCenter = Vector2(Screen.width/2, Screen.height/2); } else if(radarLocation == radarLocationValues.middleRight){ mapCenter = Vector2(Screen.width-mapWidth/2, Screen.height/2); } else if(radarLocation == radarLocationValues.bottomLeft){ mapCenter = Vector2(mapWidth/2, Screen.height - mapHeight/2); } else if(radarLocation == radarLocationValues.bottomCenter){ mapCenter = Vector2(Screen.width/2, Screen.height - mapHeight/2); } else if(radarLocation == radarLocationValues.bottomRight){ mapCenter = Vector2(Screen.width-mapWidth/2, Screen.height - mapHeight/2); } else if(radarLocation == radarLocationValues.custom){ mapCenter = mapCenterCustom; }

} </JavaScript>

An example of an AI script to interact with the radar: <JavaScript> // created by Dastardly Banana Productions // http://www.dastardlybanana.com/

//This is a dummy script to fool the radar, since our AI script is not ready to be posted at this time. // "isChasing" is true

var isChasing : boolean; var seeDistance : float = 50;

function Start () { } function Update () { var gos : GameObject[];

   gos = GameObject.FindGameObjectsWithTag("Player"); 
   //assumes only one player
   var thePlayer:GameObject = gos[0];

var dist = Vector3.Distance(thePlayer.transform.position,transform.position);

if(dist<seeDistance){ isChasing= true; } else { isChasing=false; } } </JavaScript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox