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 /
avatar image
0
Question by Cradical · Feb 17, 2014 at 05:28 PM · unity 2dradius

Unity 2D - radius/area of circle relative to camera

I'm very new to Unity and struggling to find the radius of a circle relative to the camera. I'm updating the localScale on update and want to find what percentage of the screen the circle is filling. Thoughts?

Comment
Add comment · Show 3
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 robertbu · Feb 17, 2014 at 05:39 PM 0
Share

Lot of unknown here:

  • Perspective or orthographic camera?

  • Does the distance of the circle from the camera change (if using perspective camera)?

  • Is the whole circle contained within the view?

  • Is the circle uniformly scaled?

  • Is the angle of the circle face-on to the camera?

For the simplest case, multiple the local scale by the original radius to get the new radius.

avatar image Cradical · Feb 17, 2014 at 05:50 PM 0
Share

Thank you for the response. I believe the simplest case does apply, and I will give your suggestion a try. To fill in some of those blanks:

  • orthographic - although I only have a rudimentary idea of what that means.

  • distances are static

  • yes, the whole circle is contained within the view

  • yes, they are uniformly scaled

  • yes, they are face-on to the camera

I may have just been over thinking this. I'm going to give your suggestion a whirl, thank you again!

avatar image Cradical · Feb 18, 2014 at 03:46 AM 0
Share

So this is where I landed:

     radius =  transform.renderer.bounds.size.x/4; //why 4 and not 2??? I do not understand
     area = $$anonymous$$athf.PI * $$anonymous$$athf.Pow(radius, 2);
     return area;

As the comment notes, I don't fully understand why that worked... I'm a bit concerned that it just appears correct and is not actually mathematically correct. It may just be that I don't fully understand how renderer.bounds.size works... Anyway, it seems to be working now, thanks for your help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by erick_weil · Feb 17, 2014 at 06:03 PM

okay, this only works if you circle is all in the bounds of camera. returns the percent off the circle filing,WORKS with circles... but this is not nice, sometimes the percent go up to 1, and is not very confiable

 var percent : float;
 function Update()
 {
 percent= radiusinpixels(gameObject);
 }
 function radiusinpixels(asteroid :GameObject)
 {
   var radius : Vector3;
   radius.x =renderer.bounds.size.x/2;
   var pixelradius: float =(Camera.main.WorldToScreenPoint(asteroid.renderer.bounds.center)-  Camera.main.WorldToScreenPoint(asteroid.renderer.bounds.center+radius)).magnitude;
   var pixelarea : float = Mathf.Pow(pixelradius,2)*Mathf.PI;
   var screenarea : float = (Camera.main.pixelWidth)*(Camera.main.pixelHeight);
   return (pixelarea/screenarea);
 }
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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Some questions about 2d games in unity 1 Answer

Pixel art is jittery while moving camera. 2 Answers

Jumping from Obstacle while adding velocity to Player 0 Answers

How to add to PlayerPrefs scene & mainmenu unity2d c# 0 Answers

Only one collider trigger on same prefab collision 0 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