Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rixterz · Apr 29, 2016 at 07:36 PM · c#clamporthographic camera

Fixing object position within range

Hi,

I'm working on a feature where you can mouse-pan the camera across a scene. The idea is that you cannot pan outside of the scene. There are currently 2 issues:

1) You can still pan along the x-direction and go outside the bounds (meaning the bounds are calculated incorrectly)

2) If I resize the screen, the bounds are the wrong size.

My logic for boundaries was this: if the next pan goes outside the boundary, cancel.

Here's my code, I don't know where I pulled the number 5 out of but it worked for that current screen size, and that's the problem. But I don't know what the calculation should be.

 using UnityEngine;
 using System.Collections;
 
 public class ZoomPan : MonoBehaviour
 {
 
     public float moveSensitivityX = 1.0f;
     public float moveSensitivityY = 2.0f;
     public bool updateZoomSensitivity = false;
     public float orthoZoomSpeed = 1.0f;
     public float minZoom = 10.0f;
     public float maxZoom = 40.0f;
     public bool invertMoveX = false;
     public bool invertMoveY = false;
     public float zoomMultiplier = 1f;
     
     private bool isPanning = false;
     
     private float boundaryBottom;
     private float boundaryTop;
     private float boundaryLeft;
     private float boundaryRight;
     
     void Start()
     {
         maxZoom = 80f / Screen.width * Screen.height / 2.0f;
         Camera.main.orthographicSize = maxZoom;
         boundaryBottom = Camera.main.transform.position.y - 5;
         boundaryTop = Camera.main.transform.position.y + 5;
         boundaryLeft = Camera.main.transform.position.x - 5;
         boundaryRight = Camera.main.transform.position.x + 5;
     }
     
     private Vector2 startPanPos;
     
     void Update()
     {
         
         //checkNotOutsideBounds();
         
         if(updateZoomSensitivity)
         {
             moveSensitivityX = Camera.main.orthographicSize / 10.0f;
             moveSensitivityY = Camera.main.orthographicSize / 5.0f;
         }
         
         if(!isPanning && Input.GetMouseButton(0)) //start panning
         {
             //Debug.Log("Panning");
             isPanning = true;
             startPanPos = Input.mousePosition;
         }
         else if(isPanning && !Input.GetMouseButton(0)) //stop panning
         {
             //Debug.Log("Not panning");
             isPanning = false;
         }
         else if(isPanning && Input.GetMouseButton(0)) //panning
         {
             
             Vector2 delta = new Vector2(Input.mousePosition.x - startPanPos.x, Input.mousePosition.y - startPanPos.y);
             float posX = delta.x * moveSensitivityX * zoomMultiplier * Time.deltaTime;
             posX = invertMoveX ? posX : -posX;
             
             float posY = delta.y * moveSensitivityY * zoomMultiplier * Time.deltaTime;
             posY = invertMoveY ? posY : -posY;
             
             Debug.Log(Camera.main.transform.position.x + "," + Camera.main.transform.position.y + "," + Camera.main.transform.position.z + " --> clamped within " + boundaryBottom + "|" + boundaryTop + ", " + boundaryLeft + "|" + boundaryRight);
             Vector3 newPosition = Camera.main.transform.position + new Vector3(posX, posY, posX);
             
             bool canPan = true;
             
             if(zoomMultiplier == 1f)
             {
                 if(Screen.width > Screen.height)
                 {
                     posY = 0; //clamp y-movement
                     
                 }
                 else
                 {
                     posX = 0; //clamp x-movement
                 }
                 if(newPosition.x < boundaryLeft || newPosition.x > boundaryRight)
                     canPan = false;
                 if(newPosition.y < boundaryBottom || newPosition.y > boundaryTop)
                     canPan = false;
             }
             if(canPan)
                 Camera.main.transform.position = newPosition;
             startPanPos = Input.mousePosition;
             
         }
         else if(Input.GetAxis("Mouse ScrollWheel") > 0f) //zoom in
         {
             //Debug.Log("Zoom in");
             Camera.main.orthographicSize -= orthoZoomSpeed;
             Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, minZoom, maxZoom);
             zoomMultiplier = Camera.main.orthographicSize / maxZoom;
             
  
         }
         else if(Input.GetAxis("Mouse ScrollWheel") < 0f) //zoom out
         {
             //Debug.Log("Zoom out");
             Camera.main.orthographicSize += orthoZoomSpeed;
             Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, minZoom, maxZoom);
             zoomMultiplier = Camera.main.orthographicSize / maxZoom;
             
         }
         
     }
     
 }

In the image, you can see both issues. I cannot pan any further up (there's a margin of the same size around the whole of the checkered square), and also the boundary has been incorrectly calculated on the x-axis. If my screen was larger in height than width, I would assume the boundary on the y-axis would be incorrectly calculated instead.

alt text

screenshot.png (433.0 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rixterz · Apr 29, 2016 at 05:09 PM 0
Share

As a side-note, when the scene is fully-zoomed-out, zoom$$anonymous$$ultiplier is 1 and goes down to 0.25 as the furthest zoomed in

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Zooming in and out with an orthographic camera, while the bottom edge is fixed. 0 Answers

Clamping local position 0 Answers

Clamping a Manipulated Camera 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges