itch.io is community of indie game creators and players

Devlogs

Hover physics in Unity

Mars Commander
A downloadable game for Windows

Mars Commander makes use of semi-realistic hover physics that lead to a unique gameplay experience. For example, vehicles will bounce off cliffs and reach extreme heights if you speed up a hill and press the "jump" button at precisely the right moment. It's a pretty satisfying feeling.

The hover physics are made even more prominent because of the low gravity enivronment. Mars is only about half the size of Earth, but its gravity is just a third. Playing the game, you will notice this.

Technically, physics in Unity are usually best handled in FixedUpdate, which executes code 50 times per second. The effect is wholly indepent of Unity's framerate, meaning you'll get consistent results despite whatever else may be going on in your game.  In Mars Commander, each vehicle updates its hover height in FixedUpdate using a complex and mostly realistic algorithm. 

The basis of this algorithm was not something that we invented ourselves, as we got it from this excellent YouTube video:  

Just like that video, we use Hooke's law to create our hover effect. This law of elasticity was discovered by the English scientist Robert Hook in 1660, and states that the displacement or size of a deformation is directly proportional to the deforming force or load. In practice, this means that every one of our vehicles has an ideal hover height that they will always strive towards. However, due to undulations in the terrain and other factors, such as incoming enemy projectiles, vehicles may deviate.

We tried implementing this effect in three dimensions, but this led to unexpected results, as units would always topple sooner or later. Thus, we limited the effect to one axis, meaning that only height mattered in our calculations. The effects wasn't completely realistic, but it was without a doubt the most fun solution we could devise. 

Usually, graphics are the factor that limits a game's performance most. However, in the case of Mars Commander, we noticed that CPU calculation also had a significant impact. While this wasn't only due to realistic hover physics, they certainly played a part. This is why we opted for two different means of calculation, one for when the player is near and one for when he is not. This drastically improved the game's performance.

There is muchmore to be said about the topic of hover physics, as these have also had a major impact on our AI and its setup, for example. However, that will be a topic for another time. 

Download Mars Commander
Leave a comment