Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    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:
    314
    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:
    314
    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.
     
unityunity