3D worm thing follow player

it is not 3D but the principle should be very similar. I solved it this way: