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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by arjunu · May 11, 2014 at 06:27 PM · camerainputmouse

Move camera with mouse (2D)

I want to create a map of 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. So that would be the camera's movement in X and Z axes reading input from mouse's X and Z axes. Can you help me with the script? Thanks.

This is what I have so far:

     if (Input.GetAxis("Mouse X")>0)
         {
             transform.localPosition = new Vector3 (transform.position.x,transform.position.y,transform.position.z+5);
     
         }
 
         if (Input.GetAxis("Mouse X")<0)
         {
             transform.localPosition = new Vector3 (transform.position.x,transform.position.y,transform.position.z-5);
             
         }
 
         if (Input.GetAxis("Mouse Y")<0)
         {
             transform.localPosition = new Vector3 (transform.position.x+5,transform.position.y,transform.position.z);
             
         }
 
         if (Input.GetAxis("Mouse Y")>0)
         {
             transform.localPosition = new Vector3 (transform.position.x-5,transform.position.y,transform.position.z);
             
         }

But the movement is jagged especially when moved in diagonal directions. So should I consider input from both mouse axes at the same time?

Also how do I set bounds for the camera so it won't go off the terrain?

Comment
Add comment · Show 1
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 EnigmaJetMoto · Oct 26, 2014 at 08:53 AM 0
Share

I just had to keep the rest of the C# code (using unity engine, update, etc...).

1 Reply

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

Answer by calmcarrots · May 11, 2014 at 06:45 PM

 float speed = 3f;
         transform.position += new Vector3(Input.GetAxisRaw("Mouse X") * Time.deltaTime * speed, 0f, Input.GetAxisRaw("Mouse Y") * Time.deltaTime * speed);
Comment
Add comment · Show 5 · 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 arjunu · May 12, 2014 at 05:09 AM 0
Share

anything on how to set bounds for the camera so it won't go off the terrain?

avatar image calmcarrots · May 13, 2014 at 01:53 AM 0
Share
 transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, -10f, 10f), transform.position.y, $$anonymous$$athf.Clamp(transform.position.z, -10f, 10f));
 
 Change the '-10f' and '10f' to different numbers in order to limit the position to that number. Remember to check best answer :)
avatar image EnigmaJetMoto · Oct 26, 2014 at 07:52 AM 0
Share

BOTH YOUR SCRIPTS ARE GIVING $$anonymous$$E PARSING ERRORS WTF!?

avatar image arjunu · Oct 26, 2014 at 08:18 AM 0
Share

@$$anonymous$$igmaJet$$anonymous$$oto the code is in C#. You might be writing in Unityscript.

avatar image EnigmaJetMoto · Oct 26, 2014 at 08:03 PM 0
Share

It's ok, I figured it out.

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

23 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

Related Questions

how to create a old (2D style) fps cursor 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Camera Screen to world point returns cameras transform position. 8 Answers

how can i instantiate a GameObject in mouse position and in a special distance from the camera? 2 Answers

using left right keys to turn 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