Lock Y Axis while Vector3.forward (c#)

I’ve been stuck on this for a LONG time. I have a camera (at an angle) that I use swipe gestures to move along the Z and X axis. Additionally, the camera also rotates. All solutions I’ve found use the world axis which does not work for me. I need to be able to move along the object’s Z axis (Vector3.forward) but maintain it’s current height. Has anyone faced this issue? Any recommendations? Below is what I currently have to move my camera forward and rotate:

-----Move Camera Forward-----

oldTouchPositions[0] = Input.GetTouch(0).position;

Vector2 newTouchPosition = Input.GetTouch(0).position;

transform.Translate(Vector3.forward * (oldTouchPositions[0].Value[1] - newTouchPosition.y));

-----Rotate Camera-----

transform.RotateAround(pivotPoint, Vector3.up, Mathf.Asin(Mathf.Clamp((oldTouchVector.y * newTouchVector.x - oldTouchVector.x * newTouchVector.y) / oldTouchDistance / newTouchDistance, -1f, 1f)) / 0.0174532924f);

A simple way of moving along the Forward axis of the camera but maintaining its height is to get the forward vector and set the y value of that forward vector to be the same as the height.


//Get the forward vector
Vector3 forward = camera.transform.forward;

//Set vector height same as camera height
forward = camera.transform.position.y;

//Move camera
camera.transform.position += forward * distance;

This might not work for all situations though.