object follow mouse in radius


sorry i can’t english… so this picture will help you understand

object following mouse in radius

3d space and quarter view (x, z)

i can’t write this code. help me

  1. Get mouse position into world space:

    Transform player;
    Vector3 wPos = Input.mousePosition;
    wPos.z = player.position.z-Camera.main.transform.position.z;
    wPos = Camera.main.ScreenToWorldPoint(wPos);

  2. Get direction from player to worldMousePosition

    Vector3 direction = wPos - player.position;

  3. Clamp magnitude to certain radius from player

    float radius = 5;
    direction = Vector3.ClampMagnitude(direction. radius);

  4. set object position

    Transform someObject;
    someObject.position = player.position+direction;