We consider the collective motion of animals in time-varying environments, using as a case diel vertical migration in the ocean. The animals are distributed in space such that each animal moves optimally, seeking regions which offer high growth rates and low mortalities, subject to costs on excessive movements as well as being in regions with high densities of conspecifics. The model applies to repeated scenarios such as diel or seasonal patterns, where the animals are aware of both current and future environmental conditions. We show that this problem can be viewed as a differential game of mean field type, and that the evolutionary stable solution, i.e. the Nash equilibrium, is characterized by partial differential equations, which govern the distributions and migration velocities of animals. These equations have similarities to equations that appear in the fluid dynamics, specifically the Euler equations for compressible inviscid fluids. If the environment is constant, the ideal free distribution emerges as an equilibrium. We illustrate the theory with a numerical example of vertical animal movements in the ocean, where animals are attracted to nutrient-rich surface waters while repulsed from light during daytime due to the presence of visual predators, aiming to reduce both proximity to conspecifics and swimming efforts. For this case, we show that optimal movements are diel vertical migrations in qualitative agreement with observations.