Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 BritishGuy · Nov 05, 2014 at 01:31 AM · cameramapzoomminimappanning

Camera only panning on 1 axis

Hello before i continue here is a WebPlayer Example

Zoom in with Scroll Wheel Pan around by holding right mouse button and moving mouse.

I am trying to make a world map that can be zoomed into and panned around, I have managed to add the zooming and left to right panning but for some reason it will not pan up and down,

The camera is top down orthographic so i would need to get the X and Z correct?

Here is what i have got so far, Any pointers or hints would be greatly appreciated thanks.

 using UnityEngine;
 using System.Collections;
 
 public class DragCamera : MonoBehaviour
 {
     public float dragSpeed = 2;
     private Vector3 dragOrigin;
     
     public bool cameraDragging = true;
     
     public float outerLeft = -1000f;
     public float outerRight = 1000f;
     public float outerUp = -1000f;
     public float outerDown = 1000f;
 
     public float zoomSpeed = 1000;
 
     Camera cam;
 
     void Start(){
         cam = this.camera;
     }
     
     void Update()
     {
         if (outerLeft > 0)
             outerLeft = 0;
         if (outerLeft < -19800)
             outerLeft = -19800;
 
         if (outerRight < 0)
             outerRight = 0;
         if (outerRight > 19800)
             outerRight = 19800;
 
         if (cam.orthographicSize > 20000)
             cam.orthographicSize = 20000;
         if (cam.orthographicSize < 200)
             cam.orthographicSize = 200;
 
         
         Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.z);
         
         float left = Screen.width * 0.2f;
         float right = Screen.width - (Screen.width * 0.2f);
         float up = Screen.height * 0.2f;
         float down = Screen.height - (Screen.height * 0.2f);
         
         if (mousePosition.x < left) {
             cameraDragging = true;
         }else 
         if(mousePosition.x > right) {
             cameraDragging = true;
         }
         if (mousePosition.z < up) {
             cameraDragging = true;
         }else 
         if (mousePosition.z > down) {
             cameraDragging = true;
         }
         if (Input.GetKey (KeyCode.KeypadPlus) || Input.GetAxis("Mouse ScrollWheel") > 0) {
             cam.orthographicSize -= zoomSpeed;
             outerLeft -= zoomSpeed;
             outerRight += zoomSpeed;
         }
         if (Input.GetKey (KeyCode.KeypadMinus) || Input.GetAxis("Mouse ScrollWheel") < 0) {
             cam.orthographicSize += zoomSpeed;
             outerLeft += zoomSpeed;
             outerRight -= zoomSpeed;
         }
         
         
         
         
         
         if (cameraDragging) {
             
             if (Input.GetMouseButtonDown (1)) {
                 dragOrigin = Input.mousePosition;
                 return;
             }
             
             if (!Input.GetMouseButton (1))
                 return;
             
                 Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - dragOrigin);
                 Vector3 move = new Vector3 (pos.x * dragSpeed, 0, 0);
             
             if (move.x > 0f) {
                 if (this.transform.position.x < outerRight) {
                 transform.Translate (move, Space.World);
                 }
             }else{
                 if (this.transform.position.x > outerLeft) {
                 transform.Translate (move, Space.World);
                 }
             }    
 
             if (!Input.GetMouseButton (1))
                 return;
                 
                 Vector3 pos2 = Camera.main.ScreenToViewportPoint (Input.mousePosition - dragOrigin);
                 Vector3 move2 = new Vector3 (pos2.z * dragSpeed, 0, 0);
                 
             if (move2.z > 0f) {
                 if (this.transform.position.z < outerDown) {
                     transform.Translate (move2, Space.World);
                 }
             }else{
                 if (this.transform.position.z > outerUp) {
                     transform.Translate (move2, Space.World);
                 }
 
             }
         }
     }
 }
Comment
Add comment · Show 7
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 BritishGuy · Nov 05, 2014 at 10:45 AM 0
Share

Unless there is a better way to do camera panning that i am missing?

avatar image Linus · Nov 05, 2014 at 11:14 AM 0
Share

Just glanced a the code, noticed something:

 Vector3 move2 = new Vector3 (pos2.z * dragSpeed, 0, 0);

I think should be

 Vector3 move2 = new Vector3 (0, 0, pos2.z * dragSpeed);

At line 103

And line 99 is duplicate of line 83 not that it matter much

avatar image BritishGuy · Nov 05, 2014 at 12:34 PM 0
Share

Ah yes i see that mistake on line 103, making the changes still dosn't solve the problem i am having though unfortunately, Thanks for pointing that out though, Can you see anything else? ha

avatar image Linus · Nov 05, 2014 at 12:44 PM 0
Share

Hmm what are the rotations of your camera? Perhaps you have y as "up"

avatar image Linus · Nov 05, 2014 at 01:10 PM 1
Share

Try Vector3 move2 = new Vector3 (0, pos2.z * dragSpeed, 0);

If your background, ground game object are like they usually are y becomes up for me. In top of scene set Global to Local to the transforms direction change when you rotate.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Scribe · Nov 05, 2014 at 01:05 PM

I think there are much better ways of doing this, here's a quick example of how I might go about it:

 public float dragSpeed = 2;
 Vector3 dragOrigin;
 
 public bool cameraDragging = false;
 
 public float outerLeft = -1000f;
 public float outerRight = 1000f;
 public float outerDown = -1000f;
 public float outerUp = 1000f;
 
 public float zoomSpeed = 100;
 Vector3 dir;
 
 Camera cam;
 Transform trans;
 
 void Start(){
     cam = this.camera;
     trans = this.transform;
     Clamp();
 }
 
 void Update(){
     if(Input.GetKey(KeyCode.KeypadPlus) || Input.GetAxis("Mouse ScrollWheel") > 0) {
         cam.orthographicSize -= zoomSpeed;
         outerLeft -= zoomSpeed;
         outerRight += zoomSpeed;
         outerDown -= zoomSpeed;
         outerUp += zoomSpeed;
         Clamp();
     }else if(Input.GetKey(KeyCode.KeypadMinus) || Input.GetAxis("Mouse ScrollWheel") < 0) {
         cam.orthographicSize += zoomSpeed;
         outerLeft += zoomSpeed;
         outerRight -= zoomSpeed;
         outerDown += zoomSpeed;
         outerUp -= zoomSpeed;
         Clamp();
     }
 
     if(Input.GetMouseButtonDown(1)){
         dragOrigin = Input.mousePosition;
         cameraDragging = true;
     }
     if(Input.GetMouseButtonUp(1)){
         cameraDragging = false;
     }
     if(cameraDragging){
         dir = cam.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
         Vector3 pos = trans.position;
         pos += (trans.up*dir.y + trans.right*dir.x)*dragSpeed;
         pos.x = Mathf.Clamp(pos.x, outerLeft, outerRight);
         pos.z = Mathf.Clamp(pos.z, outerDown, outerUp);
         trans.position = pos;
         
     }
 }
 
 void Clamp(){
     cam.orthographicSize = Mathf.Clamp(cam.orthographicSize, 1, 20000);
     outerLeft = Mathf.Clamp(outerLeft, -19800, 0);
     outerRight = Mathf.Clamp(outerRight, 0, 19800);
     outerDown = Mathf.Clamp(outerDown, -19800, 0);
     outerUp = Mathf.Clamp(outerUp, 0, 19800);
 }

Hope that helps!

Scribe

Comment
Add comment · Show 5 · Share
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 BritishGuy · Nov 05, 2014 at 01:10 PM 0
Share

Nice looks much cleaner than $$anonymous$$e, Yet it still does not do what im intending to do, It moves along the X axis just fine, But it dosnt move along the Z axis to go up and down, Bare in $$anonymous$$d the camera is in top down rotation 90,0,0, Thats the problem im trying to solve, Thanks.

avatar image Scribe · Nov 05, 2014 at 01:20 PM 0
Share

Edited the code slightly, should work for any camera orientation now!

avatar image BritishGuy · Nov 05, 2014 at 01:36 PM 0
Share

Nice good job, I also fixed my script too, I am still pretty new to this stuff and you writing up a better version has given me ideas of how to structure things, Thanks.

By the way if you zoom in all the way to the $$anonymous$$ orthographicSize and move the camera to one of the corners, then zoom out, it dose not reset back to the set borders, it will just zoom out in place, was that intended? Thanks again.

avatar image Scribe · Nov 05, 2014 at 01:55 PM 1
Share

not intended but easy to fix, add the following inside the clamp method:

 Vector3 pos = trans.position;
 pos.x = $$anonymous$$athf.Clamp(pos.x, outerLeft, outerRight);
 pos.z = $$anonymous$$athf.Clamp(pos.z, outerDown, outerUp);
 trans.position = pos;


That should fix it! Glad you found it useful :)

avatar image BritishGuy · Nov 05, 2014 at 02:47 PM 0
Share

Spot on thanks allot.

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

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

panning & zooming using multitouch 1 Answer

How do you differentiate between pinch to zoom with two fingers and a two finger drag? 2 Answers

minimap zooming c# 1 Answer

Minimap enemy icon help? 0 Answers

I am writing a camera script, but it seems not to be working. Any suggestions? 1 Answer


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