Radar

From Unify Community Wiki
Revision as of 21:05, 25 June 2008 by PsychicParrot (Talk | contribs)

Jump to: navigation, search

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>

@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; //*/Vector3.Angle(Vector3(0, 0, 1), centerPos,extPos);

// 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>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox