• 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 /
  • Help Room /
avatar image
1
Question by SirChick · Mar 08, 2016 at 06:26 AM · camera-movementmouse-drag

Making camera move with right click and drag

Hello

I am new to unity and game programming and am trying to get a basic script working from googling and searching the documentation.

I have wrote a script which i applied to main camera as a component, there are no syntax errors but when i run the game i do not get the expected behavior and am out of guesses on what i did wrong.

Simply i am trying to move the camera on the X & Y axis with a right click and drag motion of the mouse with a script but it did not work.

This is my script attempt, perhaps some one can see my mistake:

 using UnityEngine;
 using System.Collections;
 
 public class cameraMove : MonoBehaviour {
 
 
     // VARIABLES
     public float panSpeed = 4.0f;
 
     private Vector3 mouseOrigin;
     private bool isPanning;
 
 
     // Update is called once per frame
     void Update ()
     {
             
         mouseOrigin = Input.mousePosition;
         if (Input.GetMouseButtonDown (1)) 
         {
             //right click was pressed    
             isPanning = true;
         }
 
 
         // cancel on button release
         if (!Input.GetMouseButton (1)) 
         {
             isPanning = false;
         }
 
         //move camera on X & Y
         if (isPanning) 
         {
             Vector3 pos     = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin);
 
             // update x and y but not z
             Vector3 move     = new Vector3 (pos.x * panSpeed, pos.y * panSpeed, 0);
 
             Camera.main.transform.Translate (move, Space.Self);
         }
     }
 }
 

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

3 Replies

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

Answer by Doodums · Mar 08, 2016 at 11:56 AM

It seems you are setting your mouseOrigin variable every Update() instead of setting it once when the mouse is first clicked. That would make the line:

 Vector3 pos     = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin);

Always return a vector of (0,0,0), as you are basically just saying:

 Vector3 pos     = Camera.main.ScreenToViewportPoint (Input.mousePosition - Input.mousePosition);

Because you are setting this variable every Update():

 mouseOrigin = Input.mousePosition;

Try changing your first if to this:

          if (Input.GetMouseButtonDown (1)) 
          {
              //right click was pressed    
              mouseOrigin = Input.mousePosition;
              isPanning = true;
          }

If that doesn't help you, there are some examples here that might help you: http://forum.unity3d.com/threads/click-drag-camera-movement.39513/

Comment
Add comment · Show 1 · 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 SirChick · Mar 09, 2016 at 01:17 AM 0
Share

Ah thank you for explaining i did notice it was basically giving me 0,0,0 and i could not work out why!

avatar image
2

Answer by Naoks1 · May 10, 2017 at 11:53 PM

@Doodums I know that this is an old post, but could you explain what this code does? I'm new to programming, and just want some information on mouse position. Thanks!

Comment
Add comment · Show 1 · 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 UH4631 · Feb 06, 2019 at 07:11 AM 2
Share

Pretty late but here's step by step breakdown:

  1. When the mouse button is pushed down they store the mouse position (Input.Get$$anonymous$$ouseButtonDown(1)

  2. When the mouse is not being pressed they change the isPanning value to false, stopping the transform from updating in response to changes in the mouse's position.

  3. While the mouse is being held (Input.Get$$anonymous$$ouseButton(1)) they:

a) take the difference between the current mouse position and the mouse position they saved when the mouse was first pressed.
b) Convert the difference from screen space (2D space using pixels) to viewport space (2D space with numbers between 0 and 1)
c) Scale that difference by a factor (panSpeed) -- you may also want to normalize your offset by multiplying by time.deltaTime here
d) $$anonymous$$ake a 3D based on the 2D offset they have calculated by using the x and y coordinates of the 2D vector
e) Add the 3D offset to the camera's transform moving the camera

Input.mousePosition : The current position of the mouse in pixels
Input.Get$$anonymous$$ouseButtonDown(1)) : Returns true the frame the mouse button is first pressed on
Input.Get$$anonymous$$ouseButton(1): Returns true if the mouse button is being pressed (every frame after press)
The mouse functions take an integer that determines which mouse button is checked (0: Left-$$anonymous$$ouse, 1: Right-$$anonymous$$ouse, 2: $$anonymous$$iddle-$$anonymous$$ouse)

If you want to know what a line of code is doing try looking it up in the unity scripting reference online.

avatar image
0

Answer by mowax74 · Nov 13, 2020 at 02:07 PM

But with that solution the dragging speed is set by a multiplicator, so the mouse pointer stays not on top of the point in 3d space to the time it was clicked.

Wondering if there is a solution where after every frame of camera dragging the same spot in 3d space is below the mouse pointer. That behaviour would feel way more comfortable. With the above solution the cam movement tends to be to slow or to fast, it's not possible to adjust it to the exact movement of the mouse.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

54 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 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

Editor is lagging 0 Answers

2D How to set a limit to the rotation of my cube, when dragging with mouse 0 Answers

Make camera follow x-position of a gameobject 1 Answer

how to make the main camera follow the player in 2d 2 Answers

Camera yMin postion should be restricted to player postion 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges