Basic camera movement question

Hello,

I’m new to Unity and I already ran into my first challenge (Probably due to my weak Math background).
I’m trying to move the camera the same way the camera is moved under Unity when your are viewing the scene from the right side and the camera is rotated (e.g. 20 degrees).

In other words, when I move the camera back and forth along the Z-axis I also want to affect the Y position, in a proportional way.

In other words, a third person camera zoom(camera gets closer or further) in/out functionality.

I have managed to do it and it works except that I feel that is not done “the right way”.

This is the way I’m doing it currently:

float dx = (inputVector.x * cameraSpeed) * Time.deltaTime;
float dy = (inputVector.z * (cameraSpeed * -0.5f)) * Time.deltaTime;
float dz = (inputVector.z * cameraSpeed) * Time.deltaTime;
float x = currentCameraPosition.x + dx;
float y = currentCameraPosition.y + dy;
float z = currentCameraPosition.z + dz;
gameCamera.transform.position = new Vector3(x, y, z);

Also, where is the base of the vector representing the camera position? The origin?

Any input would be appreciated it. Thanks in advance.

Anyone? Any pointers?

Since Z is the forward vector for the camera’s position you can do something like this:

gameCamera.transform.position += (gameCamera.transform.position.forward * cameraSpeed) * Time.deltaTime;

Thanks for taking the time to answer. Unfortunately things did not work out.

Unfortunately I cannot use “gameCamera.transform.position.forward”, instead I have to do “Vector3.forward”.

However, that only moved the camera along the Z axis. That’s not what I’m trying to accomplish.
As the camera goes back it should also move up and when moving in, it should move down.

The closets I have come to archiving it (following your hint) is to do the following:

Vector3 movementVector = new Vector3(0, -0.5f, 1);
gameCamera.transform.position += (movementVector * cameraSpeed * inputVector.z) * Time.deltaTime;

What I’m trying to do is to also tight the angle of the camera.

Once again thanks for taking the time to answer. I have also reduced my code thank to your suggestion.