A multi-rotor unmanned aerial vehicle (UAV) is a rotorcraft with more than two rotors to enhance payload capability and endurance. A key feature required for the use of these vehicles under complex conditions is a technique to solve the problem of trajectory planning analytically. Hence, this paper proposes an indirect solution of the optimal control problem for path planning of the hexarotor system with different cost functions under different wind loads. First, the generalized Euler-Lagrange formulation is used to derive the dynamic equations of a hexarotor system. Hamiltonian function for a proper objective function is formed, then using the PMP optimality necessary conditions are obtained. Finally, in order to verify the effectiveness of the proposed approach, several simulation studies on a hexacopter are performed for finding the optimal paths at point-to-point motion with different objective functions like minimum effort, collision-free and windy environment. Also, a novel approach for obstacle avoidance of unmanned aerial vehicle is proposed by using a modified artificial potential field which overcomes the local minima issue and finds a practical trajectory for robot path planning. The results clearly show the effectiveness of the proposed approach on the multirotor systems.