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 /
This question was closed Jan 21, 2019 at 11:57 AM by schnein2 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by schnein2 · Jan 21, 2019 at 11:30 AM · unity 5camera-movementscrollwheel

Moving camera verticaly with mouse scrollwheel

Hello,

I want to move a camera up- and downwards using the mouse scrollwheel, I tried the following code:

 // in Start()
 cameraPositionY = transform.position.y;
         
 // in Update()
 cameraPositionY += Input.mouseScrollDelta.y * 0.1f;

I got some strange results where the transform.position.y value changed (with Debug.Log()) but the camera wasn't moving.

Your help is very much appreciated!

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

  • Sort: 
avatar image
1
Best Answer

Answer by xxmariofer · Jan 21, 2019 at 11:36 AM

CameraPositionY stores the value of the camera position, you should store a reference to the transform, or update transform.position.y to camPosiitionY

// in Update() camPositionY += Input.mouseScrollDelta.y * 0.1f; transform.position = new Vector3(transform.position.x, camPositionY , transform.position.z);

(this is not the best way just update this idea to your gameplay)

Comment
Add comment · Show 3 · 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 schnein2 · Jan 21, 2019 at 11:41 AM 0
Share

Sorry, cameraPositionY and camPositionYare the same variable.

avatar image xxmariofer schnein2 · Jan 21, 2019 at 11:44 AM 0
Share

Yes i imagined that but cameraPositionY is not the cameras position, since vector3 doesnt store information about transfom, test the code above changing camPositionY to cameraPositionY.

avatar image schnein2 xxmariofer · Jan 21, 2019 at 11:50 AM 0
Share

It works, thank you! I haven't worked with unity in long time, I should've seen that.

avatar image
3

Answer by tormentoarmagedoom · Jan 21, 2019 at 11:43 AM

Good day.

Look at your script. You are not changing the position of the camera.

At the start (this is executed only once) you define a (what i uspose is a vector2) variable called camPositionY as the current position.y of the camera.

Then...

You change the value of the vector2 variable called "camPositionY" each frame, but you are not changing the Camera.transform.position, so camera does not move, you need to change the camera postion

 void Start()
  {
  cameraPositionY = transform.position.y;
  }        

  void Update()
 {
  camPositionY += Input.mouseScrollDelta.y * 0.1f;
 Vector3 NewCameraPosition = new Vector3 (CameraObject.transform.position.x, camPositionY, CameraObject.transform.position.z)
 CameraObject.transform.position = NewCameraPosition; 
 //This last line is the command that actually moves the camera!
 }

Bye!

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 schnein2 · Jan 21, 2019 at 11:54 AM 0
Share

Thank you for your answer. I already have the solution (almost identical to yours).

Follow this Question

Answers Answers and Comments

195 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

ThirdPersonController camera Jitter when moving 1 Answer

Smoothing camera edge scroll 1 Answer

restrict camera movement, Camera moves out of bounds! 2 Answers

Camera Panning script is inconsistent 0 Answers

Camera Problem 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