Unity - limiting camera movement





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have 2D project and I want to be able move camera by touch to the right and back... I found one tutorial, moving camera with swipes so it is working fine but how can I set maximum distance to move camera?



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchController : MonoBehaviour
{
float touchStart = 0f;
Vector3 cameraDestination;
public float cameraSpeed = 0.1f;
Camera m_MainCamera;
// Use this for initialization
void Start()
{
cameraDestination = Camera.main.transform.position;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
touchStart = Input.mousePosition.x;
}
if (Input.GetMouseButtonUp(0))
{
float delta = Input.mousePosition.x - touchStart;
if (delta < -50f)
{
cameraDestination = new Vector3(Camera.main.transform.position.x + 500,
Camera.main.transform.position.y, Camera.main.transform.position.z);
// move the camera right
}
else if (delta > 50f){
cameraDestination = new Vector3(Camera.main.transform.position.x - 500,
Camera.main.transform.position.y, Camera.main.transform.position.z);
// move the camera left
}
}
if (Vector3.Distance(Camera.main.transform.position, cameraDestination) > 0.1f)
{
if (Camera.main.transform.position.x > cameraDestination.x)
{
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x - cameraSpeed,
Camera.main.transform.position.y, Camera.main.transform.position.z);
}
else
{
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + cameraSpeed,
Camera.main.transform.position.y, Camera.main.transform.position.z);
}
}
}


}










share|improve this question





























    0















    I have 2D project and I want to be able move camera by touch to the right and back... I found one tutorial, moving camera with swipes so it is working fine but how can I set maximum distance to move camera?



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class TouchController : MonoBehaviour
    {
    float touchStart = 0f;
    Vector3 cameraDestination;
    public float cameraSpeed = 0.1f;
    Camera m_MainCamera;
    // Use this for initialization
    void Start()
    {
    cameraDestination = Camera.main.transform.position;
    }
    // Update is called once per frame
    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    touchStart = Input.mousePosition.x;
    }
    if (Input.GetMouseButtonUp(0))
    {
    float delta = Input.mousePosition.x - touchStart;
    if (delta < -50f)
    {
    cameraDestination = new Vector3(Camera.main.transform.position.x + 500,
    Camera.main.transform.position.y, Camera.main.transform.position.z);
    // move the camera right
    }
    else if (delta > 50f){
    cameraDestination = new Vector3(Camera.main.transform.position.x - 500,
    Camera.main.transform.position.y, Camera.main.transform.position.z);
    // move the camera left
    }
    }
    if (Vector3.Distance(Camera.main.transform.position, cameraDestination) > 0.1f)
    {
    if (Camera.main.transform.position.x > cameraDestination.x)
    {
    Camera.main.transform.position = new Vector3(Camera.main.transform.position.x - cameraSpeed,
    Camera.main.transform.position.y, Camera.main.transform.position.z);
    }
    else
    {
    Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + cameraSpeed,
    Camera.main.transform.position.y, Camera.main.transform.position.z);
    }
    }
    }


    }










    share|improve this question

























      0












      0








      0








      I have 2D project and I want to be able move camera by touch to the right and back... I found one tutorial, moving camera with swipes so it is working fine but how can I set maximum distance to move camera?



      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;

      public class TouchController : MonoBehaviour
      {
      float touchStart = 0f;
      Vector3 cameraDestination;
      public float cameraSpeed = 0.1f;
      Camera m_MainCamera;
      // Use this for initialization
      void Start()
      {
      cameraDestination = Camera.main.transform.position;
      }
      // Update is called once per frame
      void Update()
      {
      if (Input.GetMouseButtonDown(0))
      {
      touchStart = Input.mousePosition.x;
      }
      if (Input.GetMouseButtonUp(0))
      {
      float delta = Input.mousePosition.x - touchStart;
      if (delta < -50f)
      {
      cameraDestination = new Vector3(Camera.main.transform.position.x + 500,
      Camera.main.transform.position.y, Camera.main.transform.position.z);
      // move the camera right
      }
      else if (delta > 50f){
      cameraDestination = new Vector3(Camera.main.transform.position.x - 500,
      Camera.main.transform.position.y, Camera.main.transform.position.z);
      // move the camera left
      }
      }
      if (Vector3.Distance(Camera.main.transform.position, cameraDestination) > 0.1f)
      {
      if (Camera.main.transform.position.x > cameraDestination.x)
      {
      Camera.main.transform.position = new Vector3(Camera.main.transform.position.x - cameraSpeed,
      Camera.main.transform.position.y, Camera.main.transform.position.z);
      }
      else
      {
      Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + cameraSpeed,
      Camera.main.transform.position.y, Camera.main.transform.position.z);
      }
      }
      }


      }










      share|improve this question














      I have 2D project and I want to be able move camera by touch to the right and back... I found one tutorial, moving camera with swipes so it is working fine but how can I set maximum distance to move camera?



      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;

      public class TouchController : MonoBehaviour
      {
      float touchStart = 0f;
      Vector3 cameraDestination;
      public float cameraSpeed = 0.1f;
      Camera m_MainCamera;
      // Use this for initialization
      void Start()
      {
      cameraDestination = Camera.main.transform.position;
      }
      // Update is called once per frame
      void Update()
      {
      if (Input.GetMouseButtonDown(0))
      {
      touchStart = Input.mousePosition.x;
      }
      if (Input.GetMouseButtonUp(0))
      {
      float delta = Input.mousePosition.x - touchStart;
      if (delta < -50f)
      {
      cameraDestination = new Vector3(Camera.main.transform.position.x + 500,
      Camera.main.transform.position.y, Camera.main.transform.position.z);
      // move the camera right
      }
      else if (delta > 50f){
      cameraDestination = new Vector3(Camera.main.transform.position.x - 500,
      Camera.main.transform.position.y, Camera.main.transform.position.z);
      // move the camera left
      }
      }
      if (Vector3.Distance(Camera.main.transform.position, cameraDestination) > 0.1f)
      {
      if (Camera.main.transform.position.x > cameraDestination.x)
      {
      Camera.main.transform.position = new Vector3(Camera.main.transform.position.x - cameraSpeed,
      Camera.main.transform.position.y, Camera.main.transform.position.z);
      }
      else
      {
      Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + cameraSpeed,
      Camera.main.transform.position.y, Camera.main.transform.position.z);
      }
      }
      }


      }







      unity3d swipe






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 8:52









      vb381vb381

      7119




      7119
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Your question is unclear, but I assume you want to set a limit for the camera's position. For this you'll need 2 points - the minimum point (x = the smallest X value the camera is allowed to have, y = the smallest Y value the camera is allowed to have) and the maximum point (same thing but represents the upper bound).



          Vector2 minPosition, maxPosition;


          Then, every time you move your camera, check the following conditions and only then move it:



          if (cameraDestination.x < maxPosition.x && cameraDestination.x > minPosition.x) //Ensures the camera's X value is within the allowed range
          if (cameraDestination.y < maxPosition.y && cameraDestination.y > minPosition.y) //Ensures the camera's Y value is within the allowed range





          share|improve this answer
























          • Thank you. It solved one my problem. I have few other problems, when I move some object with touch it also moves camera... some objects that should stay are moving with camera... so I will keep trying to solve this now :) thanks

            – vb381
            Nov 25 '18 at 22:04











          • Good luck, glad to help!

            – Bip901
            Nov 26 '18 at 14:31












          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%2f53465978%2funity-limiting-camera-movement%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









          2














          Your question is unclear, but I assume you want to set a limit for the camera's position. For this you'll need 2 points - the minimum point (x = the smallest X value the camera is allowed to have, y = the smallest Y value the camera is allowed to have) and the maximum point (same thing but represents the upper bound).



          Vector2 minPosition, maxPosition;


          Then, every time you move your camera, check the following conditions and only then move it:



          if (cameraDestination.x < maxPosition.x && cameraDestination.x > minPosition.x) //Ensures the camera's X value is within the allowed range
          if (cameraDestination.y < maxPosition.y && cameraDestination.y > minPosition.y) //Ensures the camera's Y value is within the allowed range





          share|improve this answer
























          • Thank you. It solved one my problem. I have few other problems, when I move some object with touch it also moves camera... some objects that should stay are moving with camera... so I will keep trying to solve this now :) thanks

            – vb381
            Nov 25 '18 at 22:04











          • Good luck, glad to help!

            – Bip901
            Nov 26 '18 at 14:31
















          2














          Your question is unclear, but I assume you want to set a limit for the camera's position. For this you'll need 2 points - the minimum point (x = the smallest X value the camera is allowed to have, y = the smallest Y value the camera is allowed to have) and the maximum point (same thing but represents the upper bound).



          Vector2 minPosition, maxPosition;


          Then, every time you move your camera, check the following conditions and only then move it:



          if (cameraDestination.x < maxPosition.x && cameraDestination.x > minPosition.x) //Ensures the camera's X value is within the allowed range
          if (cameraDestination.y < maxPosition.y && cameraDestination.y > minPosition.y) //Ensures the camera's Y value is within the allowed range





          share|improve this answer
























          • Thank you. It solved one my problem. I have few other problems, when I move some object with touch it also moves camera... some objects that should stay are moving with camera... so I will keep trying to solve this now :) thanks

            – vb381
            Nov 25 '18 at 22:04











          • Good luck, glad to help!

            – Bip901
            Nov 26 '18 at 14:31














          2












          2








          2







          Your question is unclear, but I assume you want to set a limit for the camera's position. For this you'll need 2 points - the minimum point (x = the smallest X value the camera is allowed to have, y = the smallest Y value the camera is allowed to have) and the maximum point (same thing but represents the upper bound).



          Vector2 minPosition, maxPosition;


          Then, every time you move your camera, check the following conditions and only then move it:



          if (cameraDestination.x < maxPosition.x && cameraDestination.x > minPosition.x) //Ensures the camera's X value is within the allowed range
          if (cameraDestination.y < maxPosition.y && cameraDestination.y > minPosition.y) //Ensures the camera's Y value is within the allowed range





          share|improve this answer













          Your question is unclear, but I assume you want to set a limit for the camera's position. For this you'll need 2 points - the minimum point (x = the smallest X value the camera is allowed to have, y = the smallest Y value the camera is allowed to have) and the maximum point (same thing but represents the upper bound).



          Vector2 minPosition, maxPosition;


          Then, every time you move your camera, check the following conditions and only then move it:



          if (cameraDestination.x < maxPosition.x && cameraDestination.x > minPosition.x) //Ensures the camera's X value is within the allowed range
          if (cameraDestination.y < maxPosition.y && cameraDestination.y > minPosition.y) //Ensures the camera's Y value is within the allowed range






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 15:53









          Bip901Bip901

          367




          367













          • Thank you. It solved one my problem. I have few other problems, when I move some object with touch it also moves camera... some objects that should stay are moving with camera... so I will keep trying to solve this now :) thanks

            – vb381
            Nov 25 '18 at 22:04











          • Good luck, glad to help!

            – Bip901
            Nov 26 '18 at 14:31



















          • Thank you. It solved one my problem. I have few other problems, when I move some object with touch it also moves camera... some objects that should stay are moving with camera... so I will keep trying to solve this now :) thanks

            – vb381
            Nov 25 '18 at 22:04











          • Good luck, glad to help!

            – Bip901
            Nov 26 '18 at 14:31

















          Thank you. It solved one my problem. I have few other problems, when I move some object with touch it also moves camera... some objects that should stay are moving with camera... so I will keep trying to solve this now :) thanks

          – vb381
          Nov 25 '18 at 22:04





          Thank you. It solved one my problem. I have few other problems, when I move some object with touch it also moves camera... some objects that should stay are moving with camera... so I will keep trying to solve this now :) thanks

          – vb381
          Nov 25 '18 at 22:04













          Good luck, glad to help!

          – Bip901
          Nov 26 '18 at 14:31





          Good luck, glad to help!

          – Bip901
          Nov 26 '18 at 14:31




















          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%2f53465978%2funity-limiting-camera-movement%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







          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud