Locking child position

I have a child cube GO which is moving in front of my player character. It has a script attached which makes it’s center Y position equal to the terrain height. Everything is fine until I jump with my character.

How do I prevent my child cube to move on the Y axis while the character is jumping? The script tries to update the Y position but the jump still affects it.

You can convert the cube’s position to world coords and set the world coord to be same as (world) terrain height in and Update function