Rotate one vector towards another but keeping it on the same plane

So I think this should be easy but I don’t have terminology to google the right thing. So imagine one object is on the ground and the other is flying I want to find a direction toward the flying object but so the ground object keeps moving on the same Y coordinate.
So I found one way to do this

var dir= transform.InverseTransformDirection(dir);
dir.y = 0;
dir = transform.TransformDirection(dir);

I can also maybe project direction on a plane defined by forward and right vectors of the ground object.
But I feel like there should be an easier solution any ideas?

Well to get the same behaviour as your code you can just use Vector3.ProjectOnPlane like this:

dir = Vector3.ProjectOnPlane(dir, transform.up);

This will project “dir” onto the horizontal plane relative to the given transform. Keep in mind that both, your code and this one, do not rotate the vector but project it onto the plane. That means at steeper angles the resulting vector is getting shorter. So you might want to normalize it after the projection. If you want it to have the same length as before the projection you just have to remember the magnitude before you project the vector. After the projection you can normalize the vector and multiply it by your old magnitude.