Hi! I have a very open and huge game world, multiple characters and a working network so multiple users can join and play. Now I am hung up on something very trivial - I thought. When 2 players are at a certain distance from each other I would like a gui rectangle to be drawn around the remote guy, so that 2 players are able to find each other in this big world environment (its so big - without visual help its impossible to find each other - trust me How to go about it? I know how distances checks work, that is not a problem, but how to draw the box. I would like it to be in a way that the rectangle around the remote guy gets bigger when he is closer and smaller when he is more far away (naturally). It should be easy - I know, but I am more of a networking and game-logic programmer. Should be easy anyway - Am I having some kind of blackout ? Anyone has a tip?
Here’s a piece of code which would draw it. Assuming the player gameObject is referenced by the variable player and that the player has a renderer attached.
public GameObject player;
public float margin = 0;
private Vector3[] pts = new Vector3[8];
public void OnGUI () {
Bounds b = player.renderer.bounds;
Camera cam = Camera.main;
//The object is behind us
if (cam.WorldToScreenPoint (b.center).z < 0) return;
//All 8 vertices of the bounds
pts[0] = cam.WorldToScreenPoint (new Vector3 (b.center.x + b.extents.x, b.center.y + b.extents.y, b.center.z + b.extents.z));
pts[1] = cam.WorldToScreenPoint (new Vector3 (b.center.x + b.extents.x, b.center.y + b.extents.y, b.center.z - b.extents.z));
pts[2] = cam.WorldToScreenPoint (new Vector3 (b.center.x + b.extents.x, b.center.y - b.extents.y, b.center.z + b.extents.z));
pts[3] = cam.WorldToScreenPoint (new Vector3 (b.center.x + b.extents.x, b.center.y - b.extents.y, b.center.z - b.extents.z));
pts[4] = cam.WorldToScreenPoint (new Vector3 (b.center.x - b.extents.x, b.center.y + b.extents.y, b.center.z + b.extents.z));
pts[5] = cam.WorldToScreenPoint (new Vector3 (b.center.x - b.extents.x, b.center.y + b.extents.y, b.center.z - b.extents.z));
pts[6] = cam.WorldToScreenPoint (new Vector3 (b.center.x - b.extents.x, b.center.y - b.extents.y, b.center.z + b.extents.z));
pts[7] = cam.WorldToScreenPoint (new Vector3 (b.center.x - b.extents.x, b.center.y - b.extents.y, b.center.z - b.extents.z));
//Get them in GUI space
for (int i=0;i<pts.Length;i++) pts_.y = Screen.height-pts*.y;*_
//Calculate the min and max positions
Vector3 min = pts[0];
Vector3 max = pts[0];
for (int i=1;i<pts.Length;i++) {
min = Vector3.Min (min, pts*);*
max = Vector3.Max (max, pts*);*
}
//Construct a rect of the min and max positions and apply some margin
Rect r = Rect.MinMaxRect (min.x,min.y,max.x,max.y);
r.xMin -= margin;
r.xMax += margin;
r.yMin -= margin;
r.yMax += margin;
//Render the box
GUI.Box (r,“This is a box covering the player”);
}