Vector3 with two set axis but one free one

I just started coding so I’m a bit confused and I’m also not quite sure how to explain my problem But here we go:

So I want an gameobject1 that is on top of gameobject2. I want the gameobject1 to be on the same x and z axis with the gameobject2, but have its own y axis. I can’t figure a way to do this.

I’ve tried with Vector3 but either the gameobject1’s y axis is frozen in place and cannot be moved or it doesn’t follow the gameobject2’s x and z axis.

I would really appreciate any help.

Well you just have to replace the y component of your temporary vector with your current y value.

Transform gameobject1;
Transform gameobject2;

Vector3 pos = gameobject2.position;
pos.y = gameobject1.position.y;
gameobject1.position = pos;

I’ve tried similiar things but like this one It doen’t work in the way I would like because I need the y axis to follow gravity.