Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by Grimshad · Apr 25, 2013 at 04:30 PM · radar

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
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
0
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);
     }
  
 }
Comment
Add comment · 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

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

11 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 Answers

How to import the object from server to unity 2 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