Make object follow another object while keeping the same distance from it

I want an object to follow another object just like the way it follows it when it’s parented to it, but smoothly and only in the x and z axis.

Here’s my code so far:

distanceX = player.position.x - transform.position.x;
distanceZ = player.position.z - transform.position.z;
transform.position = Vector3.Lerp(new Vector3(transform.position.x, transform.position.y, transform.position.z), new Vector3(transform.position.x + distanceX, transform.position.y, transform.position.z + distanceZ), Time.deltaTime);

Now the problem is that it goes way too close to the player and ignores the current distance.

If you got any questions please don’t hesitate to ask.

Thank you for your time!

One way would be to calculate the vector distance dependant on the angle from your player to ‘following object’

something like:

public float distanceToKeep;

private Vector3 getVecDist(float angle)
{
    Vector3 temp = Vector3.zero;
    temp.x = Mathf.Sin(angle * Mathf.Deg2Rad) * distanceToKeep;
    temp.z = Mathf.Cos(angle * Mathf.Deg2Rad) * distanceToKeep;

    // Returns change in vector based on angle and distance
    return temp;
}

Then apply the returned value to your lerping function. Note that the code is not tested and may contain errors and mistakes.