Radar

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m
Line 1: Line 1:
 +
==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 :)
 
After seeing requests for radars quite frequently in the forums, I decided to post this up for everyone to enjoy and hopefully build on :)
  
Line 18: Line 21:
 
Parrot.
 
Parrot.
  
 +
===JavaScript===
  
 
<javascript>
 
<javascript>
Line 118: Line 122:
  
 
</javascript>
 
</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>

Revision as of 17:39, 16 August 2008

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>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox