In this paper, we addressed the problem of controlling the position of a group of unicycle-type robots to follow in formation a time-varying reference avoiding obstacles when needed. We propose a kinematic control scheme that, unlike existing methods, is able to simultaneously solve the both tasks involved in the problem, effectively combining control laws devoted to achieve formation tracking and obstacle avoidance. The main contributions of the paper are twofold: first, the advantages of the proposed approach are not all integrated in existing schemes, ours is fully distributed since the formulation is based on consensus including the leader as part of the formation, scalable for a large number of robots, generic to define a desired formation, and it does not require a global coordinate system or a map of the environment. Second, to the authors’ knowledge, it is the first time that a distributed formation tracking control is combined with obstacle avoidance to solve both tasks simultaneously using a hierarchical scheme, thus guaranteeing continuous robots velocities in spite of activation/deactivation of the obstacle avoidance task, and stability is proven even in the transition of tasks. The effectiveness of the approach is shown through simulations and experiments with real robots.