Developing optimal flocking control procedure is an essential problem in mobile sensor networks (MSNs). Furthermore, finding the parameters such that the sensors can reach to the target in an appropriate time is an important issue. This paper offers an optimization approach based on metaheuristic methods for flocking control in MSNs to follow a target. We develop a non-differentiable optimization technique based on the gravitational search algorithm (GSA). Finding flocking parameters using swarm behaviors is the main contributing of this paper to minimize the cost function. The cost function displays the average of Euclidean distance of the center of mass (COM) away from the moving target. One of the benefits of using GSA is its application in multiple targets tracking with satisfying results. Simulation results indicate that this scheme outperforms existing ones and demonstrate the ability of this approach in comparison with the previous methods.