- Home /
The question is answered, right answer was accepted
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!
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)
Sorry, cameraPositionY
and camPositionY
are the same variable.
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.
It works, thank you! I haven't worked with unity in long time, I should've seen that.
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!
Thank you for your answer. I already have the solution (almost identical to yours).
Follow this Question
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