thoughts, stories and ideas

Could you explain to me how games are created?

This is a question I get a lot as a game developer.

People love playing games, both old and young. They all have new and exciting ideas, but most of them don't know how the games are made, or what it takes.

One of the most important parts of your game is its physics. It requires lot of logical thinking, balance and fine tuning to get that right, attention-grabbing feeling.

When we started developing Aero Attack, the first thing we focused during our concept phase was physics. We wanted to get it working as soon as possible.

The idea was to start working on it from the beginning and get that nice game feeling for the jump and fall mechanics.

First, we have added our main character Bob. While I was setting up our development micro-systems, my partner Kresimir was doing Bob's initial design.

public sealed class InitializePlayerSystem : IInitializeSystem {
    ...
    public void Initialize() {
        var entity = _ctx.CreateEntity();
        entity.isPlayer = true;
        entity.AddPosition(0, 0);
        entity.AddSprite("bob");
    }
}

What's next?

Bob can't just levitate. We should probably make him fall now, right?

How do we do that?

Well, Unity has something called Rigidbody component, and that component will connect our main hero Bob with the Unity's physics engine.

That means Bob will be affected by gravity.

If we give him downwards vertical velocity, it will result in the poor guy falling down indefinitely. Let's see:

For more rapid prototyping it would be nice to have Bob reset every time he goes out of screen.

Otherwise, we'll have to keep restarting our game every time we leave our keyboard to modify code or have a sip of coffee.

public sealed class ResetPlayerWhenOutOfScreen : IExecuteSystem {
    ...
    public void Execute() {
       if (_player.isOutOfScreenBounds) {
           _player.ReplacePosition(0, 0);
           _player.isBlinking = true;
       }
    }
}

Bob consistently falling in a loop looks better, right?

Now we can start thinking what kind of jump feeling we want. Do we want a sharp and fast combination, or do we need a smoother, slower jump?

Our inspiration was Flappy Bird's jump, but we wanted more control for the player.

Our goal was to create something that will be fun and addictive, but not too difficult for the user. We want players to enjoy our game after all.

After doing some research on how the Flappy Bird handles its physics, and testing out some other games with similar jump mechanics such as Jetpack Joyride, we were ready to implement our jump physics.

We had all the information we needed to start coding.

On each tap to the left side of the screen, player receives opposite vertical velocity, but we also have to take into consideration the gravity that is pulling him down.

The result is player jumping upwards, but since his downward velocity increases as he is falling, we end up with a nice balance where the player's jump strength is reduced by the force pulling him down.

Jump function is performing very well on the physics layer, but visually it's not satisfying. It's quite static and doesn't look that fun. What can we do about it?

We can easily fix this by adding a simple MonoBehaviour component to Bob. This script is tracking the character's vertical velocity from his Rigidbody component.

While Bob's vertical velocity is positive, meaning he is moving up, we will tilt Bob upwards by linearly interpolating from his current rotation to -8 degrees.

In the opposite case, when Bob's vertical velocity is negative, we tilt Bob downwards using the same linear interpolation technique, yet this time to +8 degrees.

We get Bob tilting upwards when he jumps, and downwards while he is falling.

Now this looks interesting.

It is a great time to give others your build to test your jump and see what they think. When you're working on the project for a long time, being objective becomes hard.

Having a fresh set of eyes is always needed in development, and you can always get great user experience advice.

In the end, we wanted to add a couple more details to make it look more interesting. The idea was to add ability to hold jump and have player keep moving upwards.

Our logic needed slight modifying to allow player to hold on the left side of the screen and keep jumping.

But, we had a bug, and our vertical velocity kept increasing at such rate that Bob would shoot out like a rocket after holding down jump for more that a second.

Fixing the bug was an easy task to do. All it took was capping the positive velocity, and this is the outcome:

You're all done!
This post should give you a clearer picture of how game physics in Unity work.

Hope you have enjoyed reading this post, maybe learned something new, and wish you all the luck experimenting yourself.