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
1
Question by Grimshad · Nov 10, 2014 at 01:27 AM · mousecontrollerdragclick

Click and Drag Camera

So I've created a simple camera script to click and drag the game view around. Using the left mouse button to click inside the game it "grabs" the ground and then calculates the difference between where you clicked and where you moved the mouse to, then moves the cameras position accordingly to simulate it all. The problem i'm having is that because i'm using ScreenToViewportPoint to calcuate this (I couldn't get ScreenToWorldPoint to work) every time I click it moves the camera back to 0,0,0. So everything works great as long as I don't need to click to drag more than one time... I've tried tons of things and can't get anything to work. Here's my code:

     void MousePan()
     {
         if(Input.GetMouseButtonDown(0))
         {
             panOrigin = Camera.main.ScreenToViewportPoint(Input.mousePosition);                    //Get the ScreenVector the mouse clicked
         }
 
         if(Input.GetMouseButton(0))
         {
             Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition) - panOrigin;    //Get the difference between where the mouse clicked and where it moved
             transform.position = -pos * panSpeed;                                                 //Move the position of the camera to simulate a drag, speed * 10 for screen to worldspace conversion
         }
     }
Comment
Add comment · Show 2
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 AlwaysSunny · Nov 10, 2014 at 12:20 AM 1
Share

Is there a reason you're not doing something like this ins$$anonymous$$d?

 float h = horizontalSpeed * Input.GetAxis("$$anonymous$$ouse X");
 float v = verticalSpeed * Input.GetAxis("$$anonymous$$ouse Y");
 Camera.main.transform.Translate(h, 0, v);
avatar image Grimshad · Nov 10, 2014 at 12:36 AM 0
Share

@AlwaysSunny

With that method the mouse movement doesn't translate well and you end up moving way further in the game than you actually move your mouse. The other reason is with that method, as long as your holding the mouse button the camera will keep moving.

$$anonymous$$y code was meant to make it feel like you're literally grabbing the ground and having a 1:1 movement effect on the camera.

Thanks for the response though!

3 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by Grimshad · Nov 10, 2014 at 12:34 AM

Figured it out:

         if(Input.GetMouseButtonDown(0))
         {
             bDragging = true;
             oldPos = transform.position;
             panOrigin = Camera.main.ScreenToViewportPoint(Input.mousePosition);                    //Get the ScreenVector the mouse clicked
         }
 
         if(Input.GetMouseButton(0))
         {
             Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition) - panOrigin;    //Get the difference between where the mouse clicked and where it moved
             transform.position = oldPos + -pos * panSpeed;                                         //Move the position of the camera to simulate a drag, speed * 10 for screen to worldspace conversion
         }
 
         if(Input.GetMouseButtonUp(0))
         {
             bDragging = false;
         }
Comment
Add comment · Show 3 · 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 vitorlanna · Mar 05, 2015 at 04:52 PM 0
Share

Thank you very much for posting the solution! It helped a lot =]

avatar image cac003 · Mar 13, 2018 at 08:16 PM 0
Share

Would you $$anonymous$$d providing the full script?

avatar image Content1of_akind · May 07, 2019 at 11:55 PM 0
Share

Awesome Script =)

avatar image
0

Answer by clarHandsome · Nov 13, 2018 at 05:47 AM

This is also a very good solution

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 crare · Nov 15, 2020 at 10:17 AM

Panning with using Mouse axes from input manager using Input.GetAxis().

You can inverse the pan direction by removing the minus character from transfrom.Translate()-method.

Also you may want to use z axis instead of y depending on your configuration.

 public class CameraScript : MonoBehaviour
 {
     public float panSpeed = 10f;
     
     void Update()
     {
         if (Input.GetMouseButton(1)) // right mouse button
         {
             var newPosition = new Vector3();
             newPosition.x = Input.GetAxis("Mouse X") * panSpeed * Time.deltaTime;
             newPosition.y = Input.GetAxis("Mouse Y") * panSpeed * Time.deltaTime;

             // translates to the opposite direction of mouse position.
             transform.Translate(-newPosition);
         }
     }
 
 }


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

9 People are following this question.

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

Related Questions

Bullet mouse control 2 Answers

How to check for a mouse drag 0 Answers

OnMouseUp() Click Display Effect. 1 Answer

How to disable button OnClick when drag on the screen with OnMouseDrag 2 Answers

How to properly set the position clicked by the mouse? 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