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 Rellac · Apr 15, 2014 at 01:22 PM · cameralogic

Map Panning

I have created a map system within my game. My plan is to allow the map to pan in the style of Google Maps. Click and drag, rest when not moving.

The problem I am having is that everything works wonderfully for the first drag one does, however on the second drag, the camera will poof directly to the 0,0 position before continuing on its drag again. This means that it is impossible to properly pan the camera past a small area.

I am using this script, attached to the camera, to move the camera according to the location of the map:

     deltaX = (pos.x-map.transform.position.x) - oldX;
     deltaY = (pos.y-map.transform.position.y) - oldY;
     
     oldX = (transform.position.x-map.transform.position.x);
     oldY = (transform.position.y-map.transform.position.y);
         
         
     if (Input.GetMouseButton(0) || (Input.touchCount >= 1 && Input.GetTouch(0).phase == TouchPhase.Moved))
     {
         pos = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));
         transform.position.x = (pos.x-map.transform.position.x)-deltaX;
         transform.position.y = (pos.y-map.transform.position.y)-deltaY;
     }

The map itself is initially set to the position of a GPS reading. If, for example, you are in France, the map will move down and to the left to set the france position of the map to the 0,0 point in the stage. After this, everything is static but the camera, which seems unable to properly move.

Any help would be greatly appreciated.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GrahamReeves · Apr 15, 2014 at 04:02 PM

It might be less confusing if you have less delta, old and new position persistent variables. This script attached to the camera I believe does what you want, call OnCameraMoved() when you've moved the map (In hindsight I should have called that, OnMapMoved()).

I've changed the code around so you're explicitly handling a single drag at a time instead of storing loads of positions.

 public class PanWithMouse : MonoBehaviour {
 
     public Vector3 dragStart;
     public bool wasDown = false;
     public Vector3 cameraOffset = new Vector3(10,10,10);
 
     void Start () 
     {
         OnCameraMoved();
     }
 
     void OnCameraMoved()
     {
         GameObject map = GameObject.Find ("Map");
         if (map)
         {
             cameraOffset = transform.position - map.transform.position;
         }
     }
         
     void Update () {
     
         GameObject map = GameObject.Find ("Map");
         if (!map) {
             Debug.Log ("Could not find map");
             return;
         }
 
 
         bool NowDown = Input.GetMouseButton (0);
 
         if (!wasDown && NowDown)
         {
             Debug.Log("start drag");
             dragStart = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 1));            
         } 
         else if (wasDown && NowDown) 
         {
             Vector3 DragPos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 1));    
             Vector3 Delta = dragStart - DragPos;
             Debug.Log( Delta );
             cameraOffset += Delta;
         }
         wasDown = NowDown;
 
         transform.position = map.transform.position + cameraOffset;
 
     }
 }
Comment
Add comment · 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
0

Answer by pedro_andre_oliveira · May 17, 2015 at 01:21 PM

Hello,

I'm fairly new to location-based games. I am working on a location-based game that requires access to a map; I am downloading it via WWW(url) from Google Maps (as in the example unity package) to a RAW Imagem texture field.

Is this the right way? How is pan achieved? I was thinking of having a large-scale map and zooming in by moving the camera; panning the same way. Is this the correct way?

The idea is contrary to downloading a new texture everytime I need to change the map position which requires more bandwidth.

I am also trying to implement this uncoupled of the web service, or, working with Google, Bing and Open Street Maps.

Any help is welcomed!

Comment
Add comment · 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

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

22 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

Related Questions

Camera Auto-Align with the nearest object with a determinated tag 0 Answers

Help creating restraints for an objects movement according to it's parent 1 Answer

Camera Auto-Align with the nearest object with a determinated tag 0 Answers

WebCamTexture can't play virtual camera? Couldn't config the stream!Could not find specified video device! 0 Answers

Centering camera between 4 players 2 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