Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by softrare · Jul 28, 2012 at 12:07 PM · guidisplayboxboundingbox

How to display a rectangle around a player?

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by TowerOfBricks · Jul 28, 2012 at 12:56 PM

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[i].y = Screen.height-pts[i].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[i]);
         max = Vector3.Max (max, pts[i]);
     }
     
     //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");
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image softrare · Jul 28, 2012 at 01:04 PM 0
Share

This is awesome! Thanks, exactly what I was looking for.

avatar image JasonC001 · Sep 25, 2014 at 07:13 PM 0
Share

Finally something that works. :) Thanks!

avatar image Bummie · Oct 26, 2014 at 04:29 PM 0
Share

Works like a charm, thank you!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to use two different GUI Boxes in GUI? 1 Answer

GUI Overlay Display 0 Answers

Why does the font selected for my GUI Skin not display correctly? 1 Answer

Is Mesh Collider Optimal for GUI? 2 Answers

How to check if player is hovering mouse over a GUI Box 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges