- Home /
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?
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.
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!
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.
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);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613133826im_/https://answers.unity.com/themes/thub/images/avi.jpg)