Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

distance between two points doesn't fit the scale

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

  1. kasapir

    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

    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

    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

    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.