Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KKS21199 · Mar 05, 2017 at 08:09 PM · camerainputmouse-drag

Move Camera once Mouse Drag is complete

Hi,

Here is the script I currently have. The script is attached to the camera.

Right now the moving works, if I left click and drag the mouse upwards it moves upwards. But once I rotate my camera 180 degree (there is a script which manages rotation and zoom), the camera moves downwards when mouse is upwards. And in between 0 and 180 degrees the camera goes in different directions.

Is there anyway to make the camera act like the one within Unity's Scene Editor? (with middle mouse button). Where if I click and drag downwards it moves upwards? But instead of moving X and Y axis move X and Z axis.

The TargetLookAt object is where the camera is focused on and the object itself doesn't rotate. It should just move so the camera will follow it. (the following scripts are all done and working).

  private Vector3 mouseOrigin;
     private bool isPanning;
     void Update()
     {
         
         if (Input.GetMouseButtonDown(0))
         {
             mouseOrigin = Input.mousePosition;
             //right click was pressed    
             isPanning = true;
         }
 
 
         // cancel on button release
         if (!Input.GetMouseButton(0))
         {
           //  if(isPanning)
                // MoveTarget(mouseOrigin, Input.mousePosition);
             isPanning = false;
             
         }
 
         
  
         if (isPanning)
         {
             Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
             Debug.Log(pos.ToString());
             
             Vector3 move = new Vector3(pos.x * MoveSpeed, 0, pos.y * MoveSpeed);
 
             
             TargetLookAt.transform.Translate(move, Space.World);
    
         }
         
     }

Well the solution was easy. By just rotating the TargetLookAt to the rotation of camera, the movements become aligned.

    TargetLookAt.transform.rotation = Quaternion.Euler(0, (Camera.main.transform.eulerAngles.y),      0);

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
1

Answer by toddisarockstar · Mar 06, 2017 at 04:49 AM

this script on a camera would move kinda like the unity editor camera. hopefully you can adjust from here:

     float mx;
     float my;
     Vector3 v;
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         mx = Input.GetAxis ("Mouse X");    
         my = Input.GetAxis ("Mouse Y");
         if (Input.GetMouseButton(2)) {
                         v = new Vector3 (mx, 0, my);
                         transform.position = transform.position + v * .5f;
                 }
         if (Input.GetMouseButton(1)) {
                         v = new Vector3 (my, mx, 0);
                         transform.eulerAngles = transform.eulerAngles + v;
                 }
     
 }
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 KKS21199 · Mar 06, 2017 at 05:53 AM 0
Share

Hey,

thanks. This gives the same result as before. Once I rotate the camera the controls work in the opposite direction and in between rotations the controls are completely a mess. The camera is rotated via Transform.LookAt and a distance between the the TargetLookAt and Camera is kept by the scripts as well. Both the camera and TargetLookAt are free objects without any parent or child.

https://i.gyazo.com/d572268e670bf98aec6545df2489165f.mp4 (See how the camera moves in different direction once I turn 90 degrees)

avatar image
1

Answer by eldruz · Mar 06, 2017 at 01:23 PM

As an alternative, you could also replace the final line of the script with :

 TargetLookAt.transform.Translate(move, Camera.main.transform);

This way, the translation is always relative to the camera local coordinates system.

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

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

I need to make the Camera Quit snapping to center... 1 Answer

ps3 controller messed up the normal controls 1 Answer

Help Ship Controll 0 Answers

rotate with alt doesnt work 15 Answers

Simple Object Dragging 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