How to make limits stay the same even when changing sizes/resolutions?










1















This part of the script limits movement on x and y so the camera can't go outside of the game scene and it works on my 2d ortographic camera.



var pos = transform.position;
transform.position = new Vector2(
Mathf.Clamp(pos.x, xPanMax, xPanMin),
Mathf.Clamp(pos.y, yPanMax, yPanMin));


When I change the screen size the limits are all off and I have to readjust the numbers for it not to leave the games boundaries or being able to pan around the whole scene.



Is there any way to change this code so the limits stay the same even when changing sizes/resolutions?










share|improve this question




























    1















    This part of the script limits movement on x and y so the camera can't go outside of the game scene and it works on my 2d ortographic camera.



    var pos = transform.position;
    transform.position = new Vector2(
    Mathf.Clamp(pos.x, xPanMax, xPanMin),
    Mathf.Clamp(pos.y, yPanMax, yPanMin));


    When I change the screen size the limits are all off and I have to readjust the numbers for it not to leave the games boundaries or being able to pan around the whole scene.



    Is there any way to change this code so the limits stay the same even when changing sizes/resolutions?










    share|improve this question


























      1












      1








      1








      This part of the script limits movement on x and y so the camera can't go outside of the game scene and it works on my 2d ortographic camera.



      var pos = transform.position;
      transform.position = new Vector2(
      Mathf.Clamp(pos.x, xPanMax, xPanMin),
      Mathf.Clamp(pos.y, yPanMax, yPanMin));


      When I change the screen size the limits are all off and I have to readjust the numbers for it not to leave the games boundaries or being able to pan around the whole scene.



      Is there any way to change this code so the limits stay the same even when changing sizes/resolutions?










      share|improve this question
















      This part of the script limits movement on x and y so the camera can't go outside of the game scene and it works on my 2d ortographic camera.



      var pos = transform.position;
      transform.position = new Vector2(
      Mathf.Clamp(pos.x, xPanMax, xPanMin),
      Mathf.Clamp(pos.y, yPanMax, yPanMin));


      When I change the screen size the limits are all off and I have to readjust the numbers for it not to leave the games boundaries or being able to pan around the whole scene.



      Is there any way to change this code so the limits stay the same even when changing sizes/resolutions?







      unity3d camera






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 18:28









      Community

      11




      11










      asked Nov 15 '18 at 11:17









      Toni MarinoToni Marino

      82




      82






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Yes, use Cinemachine. It has dynamic understanding of the frame and scene and view.



          You can learn much more about it here:https://docs.unity3d.com/Packages/com.unity.cinemachine@2.1/manual/index.html



          Cinemachine is a free addon package for 2018.1 and up.






          share|improve this answer























          • Thanks for the answer, I looked cinemachine up, but the moment I add a cinemachine 2d camera, my script stops working until i disable the camera again. Any idea why this is happening?

            – Toni Marino
            Nov 15 '18 at 21:44












          • You'll have to rewrite your script to operate on vcams, the Cinemachine variation of cameras. It's not a straight plugin and go, you'll need to learn a bit about the ways Cinemachine works, and how it overcomes the (massive) limitations of the Unity camera. Well worth it, because you'll find all sorts of things you can do with Cinemachine that are really easy to do with it, because it's designed to be a camera system. It is, IMNSHO, the most essential thing to add into Unity.

            – Confused
            Nov 16 '18 at 1:45










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318281%2fhow-to-make-limits-stay-the-same-even-when-changing-sizes-resolutions%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Yes, use Cinemachine. It has dynamic understanding of the frame and scene and view.



          You can learn much more about it here:https://docs.unity3d.com/Packages/com.unity.cinemachine@2.1/manual/index.html



          Cinemachine is a free addon package for 2018.1 and up.






          share|improve this answer























          • Thanks for the answer, I looked cinemachine up, but the moment I add a cinemachine 2d camera, my script stops working until i disable the camera again. Any idea why this is happening?

            – Toni Marino
            Nov 15 '18 at 21:44












          • You'll have to rewrite your script to operate on vcams, the Cinemachine variation of cameras. It's not a straight plugin and go, you'll need to learn a bit about the ways Cinemachine works, and how it overcomes the (massive) limitations of the Unity camera. Well worth it, because you'll find all sorts of things you can do with Cinemachine that are really easy to do with it, because it's designed to be a camera system. It is, IMNSHO, the most essential thing to add into Unity.

            – Confused
            Nov 16 '18 at 1:45















          1














          Yes, use Cinemachine. It has dynamic understanding of the frame and scene and view.



          You can learn much more about it here:https://docs.unity3d.com/Packages/com.unity.cinemachine@2.1/manual/index.html



          Cinemachine is a free addon package for 2018.1 and up.






          share|improve this answer























          • Thanks for the answer, I looked cinemachine up, but the moment I add a cinemachine 2d camera, my script stops working until i disable the camera again. Any idea why this is happening?

            – Toni Marino
            Nov 15 '18 at 21:44












          • You'll have to rewrite your script to operate on vcams, the Cinemachine variation of cameras. It's not a straight plugin and go, you'll need to learn a bit about the ways Cinemachine works, and how it overcomes the (massive) limitations of the Unity camera. Well worth it, because you'll find all sorts of things you can do with Cinemachine that are really easy to do with it, because it's designed to be a camera system. It is, IMNSHO, the most essential thing to add into Unity.

            – Confused
            Nov 16 '18 at 1:45













          1












          1








          1







          Yes, use Cinemachine. It has dynamic understanding of the frame and scene and view.



          You can learn much more about it here:https://docs.unity3d.com/Packages/com.unity.cinemachine@2.1/manual/index.html



          Cinemachine is a free addon package for 2018.1 and up.






          share|improve this answer













          Yes, use Cinemachine. It has dynamic understanding of the frame and scene and view.



          You can learn much more about it here:https://docs.unity3d.com/Packages/com.unity.cinemachine@2.1/manual/index.html



          Cinemachine is a free addon package for 2018.1 and up.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 13:54









          ConfusedConfused

          3,09741953




          3,09741953












          • Thanks for the answer, I looked cinemachine up, but the moment I add a cinemachine 2d camera, my script stops working until i disable the camera again. Any idea why this is happening?

            – Toni Marino
            Nov 15 '18 at 21:44












          • You'll have to rewrite your script to operate on vcams, the Cinemachine variation of cameras. It's not a straight plugin and go, you'll need to learn a bit about the ways Cinemachine works, and how it overcomes the (massive) limitations of the Unity camera. Well worth it, because you'll find all sorts of things you can do with Cinemachine that are really easy to do with it, because it's designed to be a camera system. It is, IMNSHO, the most essential thing to add into Unity.

            – Confused
            Nov 16 '18 at 1:45

















          • Thanks for the answer, I looked cinemachine up, but the moment I add a cinemachine 2d camera, my script stops working until i disable the camera again. Any idea why this is happening?

            – Toni Marino
            Nov 15 '18 at 21:44












          • You'll have to rewrite your script to operate on vcams, the Cinemachine variation of cameras. It's not a straight plugin and go, you'll need to learn a bit about the ways Cinemachine works, and how it overcomes the (massive) limitations of the Unity camera. Well worth it, because you'll find all sorts of things you can do with Cinemachine that are really easy to do with it, because it's designed to be a camera system. It is, IMNSHO, the most essential thing to add into Unity.

            – Confused
            Nov 16 '18 at 1:45
















          Thanks for the answer, I looked cinemachine up, but the moment I add a cinemachine 2d camera, my script stops working until i disable the camera again. Any idea why this is happening?

          – Toni Marino
          Nov 15 '18 at 21:44






          Thanks for the answer, I looked cinemachine up, but the moment I add a cinemachine 2d camera, my script stops working until i disable the camera again. Any idea why this is happening?

          – Toni Marino
          Nov 15 '18 at 21:44














          You'll have to rewrite your script to operate on vcams, the Cinemachine variation of cameras. It's not a straight plugin and go, you'll need to learn a bit about the ways Cinemachine works, and how it overcomes the (massive) limitations of the Unity camera. Well worth it, because you'll find all sorts of things you can do with Cinemachine that are really easy to do with it, because it's designed to be a camera system. It is, IMNSHO, the most essential thing to add into Unity.

          – Confused
          Nov 16 '18 at 1:45





          You'll have to rewrite your script to operate on vcams, the Cinemachine variation of cameras. It's not a straight plugin and go, you'll need to learn a bit about the ways Cinemachine works, and how it overcomes the (massive) limitations of the Unity camera. Well worth it, because you'll find all sorts of things you can do with Cinemachine that are really easy to do with it, because it's designed to be a camera system. It is, IMNSHO, the most essential thing to add into Unity.

          – Confused
          Nov 16 '18 at 1:45



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318281%2fhow-to-make-limits-stay-the-same-even-when-changing-sizes-resolutions%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto