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
1
Question by Maru07 · May 14, 2012 at 11:20 PM · camerapositionvector3

Moving the Camera Up and Down by pressing Key

Hey Guys I have the following problem, The Script I have

var lookSpeed = 15.0; var moveSpeed = 15.0; var rotationX = 0.0; var rotationY = 0.0; function Update () { rotationX += Input.GetAxis("Mouse X")*lookSpeed; rotationY += Input.GetAxis("Mouse Y")*lookSpeed; rotationY = Mathf.Clamp (rotationY, -90, 90);

 transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
 transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);

 transform.position += transform.forward*moveSpeed*Input.GetAxis("Vertical");
 transform.position += transform.right*moveSpeed*Input.GetAxis("Horizontal");
 transform.position += transform.up*moveSpeed*Input.GetAxis("Vertical");
 transform.position += transform.down*moveSpeed*Input.GetAxis("Horizontal");

only moves the camera in any direction but I need the Camera to move positive along the Y Axis when pressing Q for example and Negative when pressing E. Thx for the Answers =)

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
Best Answer

Answer by aldonaletto · May 15, 2012 at 12:50 AM

You could replace the movement code (the last 4 lines) with this code:

    ...
    transform.position += transform.forward*moveSpeed*Input.GetAxis("Vertical")*Time.deltaTime;
    transform.position += transform.right*moveSpeed*Input.GetAxis("Horizontal")*Time.deltaTime;
    var vDir: float = 0;
    if (Input.GetKey("q")) vDir = 1;
    if (Input.GetKey("e")) vDir = -1;
    transform.position += transform.up*moveSpeed*vDir*Time.deltaTime;
Notice the multiplication by Time.deltaTime: this makes the movement framerate independent, a very important thing given the wide performance variation among different machines (the velocity moveSpeed means units per second in this case). This isn't necessary in the rotation section, because Mouse X and Mouse Y already give values that compensate the framerate.
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 Maru07 · May 15, 2012 at 10:12 AM 0
Share

Wow thx again :D working Perfectly

avatar image
0

Answer by hathol · May 15, 2012 at 12:44 AM

Try this:

  if (Input.GetKey(KeyCode.Q))
      transform.position += Vector3.up * moveSpeed * Time.deltaTime;
  else if (Input.GetKey(KeyCode.E))
      transform.position += -Vector3.up * moveSpeed  * Time.deltaTime;

Vector3.up (and respectively .forward and .right) gives you a unit vector (direction) in world space coordinates, unlike transform.up which gives you the direction in local space.

Tip: Always multiply your movements with the current delta time to make it framerate independent

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect VR camera movement? 0 Answers

How to keep the same position of the object instead of camera 0 Answers

Child GameObject is aligned to world axis and not the parents axis (UPDATE) 1 Answer

Trouble converting transform.position to C# 1 Answer

Any help with prevent camera from passing a specific coordinate? 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