How to make camera follow player smoothly on X axes but not on y axis?

Hi guys,

I have a camera script that follows my player smoothly using “transform.position = Vector3.Lerp…” all the time.

Now I put in the scene cannon gun that shoots my player realy fast upwards (only on y axes) and my camera is lazy to follow the player at this point. I have already tryed to increase follow speed or parent the camera to my player but the camera always jumps on the player - that is not what I want.

I want the camera to be parented smoothly to the moving object - to the player. Is it even possible? How should I solve this??

Or even better - to follow my player smoothly on X axes but to be instant on Y axes with smooth aim?

Now I have this (following on X is smooth but the camera jumps on Y axes when targets the player):

float xPos = Mathf.Lerp(transform.position.x, player.transform.position.x, smoothSpeed * Time.deltaTime);
transform.position = new Vector3(xPos, player.transform.position.y, -10);

Please help me…

I’m not sure I follow you… Do you want your camera to follow the player in both axis but smooth the camera movement in the x axis only? Or do you only want to follow the player in the X axis and let the y position of the camera untouched?

If it’s the first one I think your code should work, so I’m guessing you want to do the second thing. In that case, why are you using the player y position when updating the camera’s position? This should follow only in the X axis:

transform.position = new Vector3(xPos, transform.position.y, -10);