# distance between two points doesn't fit the scale

Discussion in 'Scripting' started by kasapir, Mar 13, 2018.

1. ### kasapir

Joined:
Mar 8, 2018
Posts:
8
hey,
I'm trying to fit a template's background to the camera field of view.
so I found the "Square" FOV points
and calculated the distance - widthand height.
but in reality when I'm putting the result at scale x and y - it's not the real lengths
why is that? how can i fix it or calculate the scale i need from the points i already have?

thanks a lot.

2. ### JoeStrout

Joined:
Jan 14, 2011
Posts:
6,999
Well you haven't shown us your code, so we can't be specific about how to fix it.

It's probably just some combination of (1) you haven't realized that FOV refers to the vertical field of view, not the horizontal, or (2) you have some errors in your math.

3. ### kasapir

Joined:
Mar 8, 2018
Posts:
8
downLeftRAY = Camera.main.ScreenPointToRay(Vector3.zero);
topRightRAY = Camera.main.ScreenPointToRay(new Vector3(Screen.width, Screen.height));

downLeft = new Vector3(downLeftRAY.origin.x, downLeftRAY.origin.y, 13);
topRight = new Vector3(topRightRAY.origin.x, topRightRAY.origin.y, 13);
downRight = new Vector3(topRight.x, downLeft.y, 13);
topLeft = new Vector3(downLeft.x, topRight.y, 13);

var ScaleX = Vector3.Distance(downLeft, downRight);
var ScaleY = Vector3.Distance(downLeft, topLeft);

obj.transform.localScale = new Vector3(ScaleX, ScaleY, 1);
obj.transform.localPosition = new Vector3(downLeft.x, downLeft.y, 13);

thank u.
THIS IS THE CODE.
but the scaleX, scaleY is not giving me the result I want
how do I need to calculate it right?

4. ### JoeStrout

Joined:
Jan 14, 2011
Posts:
6,999
You're trying to make the object fill the view? Just use trigonometry. Sketch out a right triangle from the camera, to this far object, with an angle equal to fieldOfView, and the desired distance for the adjacent leg; what you need to calculate is the far side of the triangle, which is related to the adjacent side and the angle via the Tangent function (tan(angle) = opposite/adjacent).

So, something like this:

Code (csharp):
1.
2. float neededHeight = Mathf.Atan(Camera.main.fieldOfView/2) * distance * 2;
3.
where distance is the distance from the camera to this background object, and neededHeight gives you how big that object should be. If your object is modeled with a height of 1, then this is simply the vertical scale you need.

If you need to also fill the view horizontally, then scaleY should be simply scaleX * Camera.main.aspect.