How to make a transform follow a target transform on it's local z axis only?

I’m trying to make Object A follow Object B, but I need Object A to only follow on it’s local forward z axis, and retain it’s initially set x and y axis. I thought this would be an easy solution, but I’m having difficulty.

I’ve only seen posts that say to use transform.forward * Time.deltatime. But this will not work in my case since I need it to follow Object B.

I have tried:

Vector3 followPosition = transform.InverseTransformPoint(targetPosition);

followPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, followPosition.z);

transform.localPosition = followPosition;

Thank you!

transform.position += Vector3.Project(targetPosition - transform.position, transform.forward);