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 vahid62 · Nov 12, 2014 at 09:26 PM · cameradragmap

[Solved] Move camera with mouse dragging (2D)

Hi,

I want to create a map for my 3D scene. So I have placed a camera in the sky looking down. When M is pressed, I'm switching from the character controller camera to this camera. Now I want to move this camera in up, down, left and right directions with mouse's movement and change height of camera with scrolling middle mouse button. I have this script

 var camera1 : Camera; 
 var camera2 : Camera; 
 
 function FixedUpdate() 
 {
 
     if (Input.GetKeyDown (KeyCode.M))
     {
         GameObject.Find("First Person Controller").GetComponent("SmoothMouseLook").enabled = false;
         GameObject.Find("First Person Controller").GetComponent("FPSWalker").enabled = false;
         
         camera1.enabled=true;//2D camera
         camera2.enabled = false;//first person camera
         
         moveDirection = new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y"));
         camera1.transform.position += moveDirection;
     }
 }

But 2d camera (camera1) doesn't move when I drag the mouse. What's the problem?

Comment
Add comment · Show 3
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 jan09 · Nov 12, 2014 at 10:21 PM 1
Share

Don't use input in fixedUpdate, do it in update

avatar image vahid62 · Nov 12, 2014 at 10:39 PM 0
Share

I used Update and And the problem is still there.

avatar image jan09 · Nov 12, 2014 at 10:45 PM 0
Share

Use Debug.log if any input is accepted...

1 Reply

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

Answer by robertbu · Nov 13, 2014 at 05:36 AM

Note this is not a good way to approach dragging with the mouse. Drag and drop code of various sorts have been posted to Unity Answers a number of times, so I suggest you seek out those answers as a starting point.

As for your code, you are using Input.GetKeyDown(). GetKeyDown() fires for only a single frame, so your code above will only move the camera if you happen to be moving the mouse at the very same time the key goes down, and it will only move it once per key press.

As other have mentioned, execute this code in Update(). In particular you don't want to exectue GetKeyDown() in FixedUpdate() since the difference in frame rates between Update() and FixedUpdate() can cause you to lose key presses.

Note you are using GetAxis() return values to move world objects. There is no match between the two coordinate systems.

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 vahid62 · Nov 13, 2014 at 11:43 AM 0
Share

Thanks robertbu, I found it on this page that you answered.

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

27 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

Related Questions

Minimap enemy icon help? 0 Answers

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

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

UnityPlayer in an Android MapActivity 3 Answers

Different Camera changes work against each other? 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