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 MorphingDragon · Jun 25, 2013 at 03:43 PM · cameradragmoveable

Dragging Camera Speed

I'm making a strategy game and I would like the player to be able to drag the camera by clicking on the background and moving the cursor. I have the dragging functionality implemented but I'm having trouble getting the camera to move at the same speed as the cursor. The functionality I want is that when speed = 1.0, the camera moves at the same speed as the cursor. The user can then customize the scroll speed in the settings.

    void Update()
         {
             if (mouseDown)
             {
                 // Get the current mouse
                 Vector2 currentMouse = Input.mousePosition;
                 
                 // Calculate delta
                 Vector2 delta = Camera.main.ScreenToWorldPoint(-oldMouse) + Camera.main.ScreenToWorldPoint(currentMouse);
                 print(delta);
                 Vector3 dV = new Vector3(delta.x * speed, 0, delta.y * speed);
     
     
                 // Move the camera
                 Camera.main.transform.position += dV;
     
                 oldMouse = currentMouse;
             }
         }
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 robertbu · Jun 26, 2013 at 03:59 AM 0
Share

You are close here. When dealing with perspective and tracking, you have to say how far in front of the camera. In particular, when you call ScreenToWorldPoint(), the 'Z' parameter should be the distance in front of the camera.

avatar image moha · Jun 26, 2013 at 09:04 AM 0
Share

if you want you can do it in a different way. in the OnGUI

if(Event.current.type == EventType.mouseDrag ) {

 Camera.transform.Translate(new Vector3(Event.current.delta.x *-1,Event.current.delta.y ,0) * Time.deltaTime * factor);
         

}

you just have to change the factor value to change the speed

1 Reply

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

Answer by amphoterik · Jun 26, 2013 at 12:01 PM

The mouse input is always given in delta (that is, the amount it has changed since last frame). Therefore, moving the map is easy:

 float dx = Input.GetAxis("Mouse X");
 float dy = Input.GetAxis("Mouse Y");

And then moving the ground by that much. The problem stems from the fact that your ground is a distance away and you are probably in perspective mode. That will make the ground seem slower. What you need to do is determine a coefficient by which the ground at a certain distance will move at the same speed as the mouse. This is a trial and error thing.

If your camera can be at different distances from the ground, then you will need to know different values for the distance.

Now, you seem to be doing a lot of that already (kind of makes my rant moot, but alas). The main difference though, is that you want to user to be able to change it, but you also will need a speed coefficient. That again is easy to do. You will just need to make sure your coefficient brings your terrain up to 1:1 speed and then your speed reduces or increases it from there:

 //my style from above
 float dx = Input.GetAxis("Mouse X");
 float dy = Input.GetAxis("Mouse Y");
 Vector3 dV = new Vector3(-dx * coef * speed, 0, dy * coef * speed);
 //your style
 Vector3 dV = new Vector3(-delta.x * coef * speed, 0, delta.y * coef * speed);

The minus was also added to make it pan in the correct direction.

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

17 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

Related Questions

Quaternion - Euler Angles, weird variable swapping when converting 2 Answers

Make object follow mouse, while actually moving camera, instead of object 2 Answers

Mouse Orbit + Move Object + Follow Problem 1 Answer

Drag Object, same speed as mouse/touch 3 Answers

Can you use UI Scroll Rect for camera movement? 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