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!

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!

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)