- Home /
Edge of Screen Radar
I rewrote PsychicParrots' radar script to work for my game, but I'm not sure how to finish it.
I need the blips to stay on the edge of the radar rect boundries until the object is visible by the main camera and then stop drawing that blip. I'm sure I know how to have it stop drawing the blip once it's visible by the main camera, but I'm horrible at trigonometry and have no idea how to change this code to tell the blip to stay on the outer most edge of the rect instead of it's scale replica location within the rect (current setup).
@script ExecuteInEditMode()
//Radar Script adapted from PsychicParrots' radar script
//Variables
var blipTexture :Texture;
var mapScaleX :float = 12;
var mapScaleY :float = 8.5;
//Private Variables
private var mapCenter = Vector2(Screen.width * 0.5,Screen.height * 0.5);
//Draw GUI
function OnGUI()
{
//Draw blips for asteroids
DrawBlipsForAsteroids();
}
function DrawBlip(asteroid,aTexture)
{
var centerPos = transform.position;
var extPos = asteroid.transform.position;
//First we need to get the distance of the asteroid from the center
var dist = Vector3.Distance(centerPos,extPos);
//Don't draw asteroids at ranges greater than this
if(dist <= 30)
{
var dx = centerPos.x - extPos.x; // how far to the side of the center is the asteroid?
var dz = centerPos.z - extPos.z; // how far in front or behind the center is the asteroid?
//What's the angle to turn to face the asteroid - compensating for the center rotation?
var deltay = Mathf.Atan2(dx,dz)*Mathf.Rad2Deg - 270 - transform.eulerAngles.y;
//Just basic trigonometry to find the point x,y (asteroids location) given the angle deltay
var bX = dist*Mathf.Cos(deltay * Mathf.Deg2Rad);
var bY = dist*Mathf.Sin(deltay * Mathf.Deg2Rad);
bX = bX * mapScaleX; // scales down the x-coordinate so that the plot stays within our radar
bY = bY * mapScaleY; // scales down the y-coordinate so that the plot stays within our radar
//Draw the blips within the constraints of the radar
GUI.DrawTexture(Rect(mapCenter.x + bX,mapCenter.y + bY,2,2),aTexture);
}
}
function DrawBlipsForAsteroids()
{
//Find all game objects tagged asteroid
var asteroids : GameObject[];
asteroids = GameObject.FindGameObjectsWithTag("asteroid");
//Iterate through them and call drawBlip function
for (var asteroid : GameObject in asteroids)
{
DrawBlip(asteroid,blipTexture);
}
}
Comment
Best Answer
Answer by Grimshad · Apr 25, 2013 at 06:17 PM
Alright, I got it working. I think it could probably be done better, but it works. So if anyone else ever needs it here it is:
@script ExecuteInEditMode()
//Radar Script adapted from PsychicParrots' radar script
//Variables
var blipTexture :Texture;
var mapScaleX :float = 12;
var mapScaleY :float = 8.5;
var radarToggle :boolean = false;
//Private Variables
private var mapCenter = Vector2(Screen.width * 0.5,Screen.height * 0.5);
//Draw GUI
function OnGUI()
{
//Draw blips for asteroids
if(radarToggle)
DrawBlipsForAsteroids();
}
function DrawBlip(asteroid,aTexture)
{
var centerPos = transform.position; //NOTE: If this is the position of the center of radar data collection
//if the game is not static, reference the player object
var extPos = asteroid.transform.position;
//First we need to get the distance of the asteroid from the center
var dist = Vector3.Distance(centerPos,extPos);
//Don't draw asteroids at ranges greater than this
if(dist <= 30)
{
var dx = centerPos.x - extPos.x; // how far to the side of the center is the asteroid?
var dz = centerPos.z - extPos.z; // how far in front or behind the center is the asteroid?
var xpos;
var ypos;
//These if statements essentially check which direction the astroid is from the center
//and set the position of the blip near the edge of the correct side of the screen
if(dx > 0)
xpos = 20;
else
xpos = Screen.width - 20;
if(dz > 0)
ypos = Screen.height - 20;
else
ypos = 20;
//What's the angle to turn to face the asteroid - compensating for the center rotation?
var deltay = Mathf.Atan2(dx,dz)*Mathf.Rad2Deg - 270 - transform.eulerAngles.y;
//Just basic trigonometry to find the point x,y (asteroids location) given the angle deltay
var bX = dist*Mathf.Cos(deltay * Mathf.Deg2Rad);
var bY = dist*Mathf.Sin(deltay * Mathf.Deg2Rad);
bX = bX * mapScaleX; // scales down the x-coordinate so that the plot stays within our radar
bY = bY * mapScaleY; // scales down the y-coordinate so that the plot stays within our radar
//Draw the blips within the constraints of the radar
//Depending on which direction the asteroid is, let it move along the correct axis and lock the other axis in place
if(dx > 12.5 || dx < -12.5)
{
GUI.DrawTexture(Rect(xpos,mapCenter.y + bY,2,2),aTexture);
}
else if(dz > 9.3 || dz < -9.3)
{
GUI.DrawTexture(Rect(mapCenter.x + bX,ypos,2,2),aTexture);
}
}
}
function DrawBlipsForAsteroids()
{
//Find all game objects tagged asteroid
var asteroids : GameObject[];
asteroids = GameObject.FindGameObjectsWithTag("asteroid");
//Iterate through them and call drawBlip function
for (var asteroid : GameObject in asteroids)
{
//Don't draw a blip if the asteroid is in the game view
if (!asteroid.renderer.isVisible)
DrawBlip(asteroid,blipTexture);
}
}