Aiming at the problem of UAV formation's obstacle avoidance and the consensus of position and velocity in a 3D obstacle environment, a novel distributed obstacle avoidance control algorithm for cooperative formation based on the improved artificial potential field (IAPF) and consensus theory is proposed in this paper. First, the particle model of the UAV and the dynamic model of the second-order system are established, and the topological structure of the communication network of the system is described with the knowledge of graph theory. Second, the attractive potential field function containing the coordination gains factor, the repulsive potential field function containing the influence factor of the repulsive force and the planning angle, and the potential field function between the UAVs containing the communication weight are defined. Then, the variables of position and velocity in the consensus protocol are improved by the reference vector of the formation center and the expected velocity, respectively, and a new formation obstacle avoidance control protocol is designed by combining the IAPF and the theory of consensus. Finally, the Lyapunov function is used to prove the stable convergence of the algorithm. The simulation results show that this method can not only prevent the UAV from colliding with each other while avoiding static and dynamic obstacles but also enable the UAV to quickly restore the expected formation and achieve the consensus of the relative distance, relative height, and velocity.