Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Leftalignement of orthogonal camera with different aspect ratios

Discussion in 'Scripting' started by zlSimon, Jul 12, 2018.

  1. zlSimon

    zlSimon

    Joined:
    Apr 11, 2013
    Posts:
    31
    I have an orthographic camera which looks at a huge map (consisting of several sprites) where you can move the camera around to see the whole thing.

    This should work in every possible aspect ratio.
    The initial setup of the camera position is done in a 4:3 aspect ratio.

    The problem is that initially the camera view should always be at the bottom left, aligned to the left end of the map. Since unity keeps the height and changes the width equally to both sides I have to move the camera for each aspect ratio to the left or right.

    How can I calculate the new x value of the camera position which I have to move the camera to dependent of a aspect ratio change?
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    I'm not sure if there is a more elegant way, but you could cast two rays from your Camera onto the surface with the distance of one pixel, the resulting points will give you the world distance of one pixel. Then cast a ray from the edge position onto your camera / screen space, which will give you the offset in pixels to the desired location (bottom left in your case). With the needed pixel distance and the conversion into world units you can move the camera.
    In my thoughts it works ;-)
     
  3. zlSimon

    zlSimon

    Joined:
    Apr 11, 2013
    Posts:
    31
    hm well this could work :) not sure how to cast a 2 rays with the distance of one pixel...

    But there has to be an easier solution...
     
  4. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    When using ScreenToWorldPoint you are providing a Vector that is actually the pixels. So simply use 0,0 and 0,1 and there you go.