Second group project - Mobile game in Unity

The course was a mobile game combined with back-end course.

The main game loop we were assigned was to make an AR Game, which actaully uses real steps from the player.
Like if you walk x amount of steps in the game, you clear a region and get awards.

My responsibilities became the following:

  • Implementing the a Step Counter mechanic
  • Implementing the backend database with the game
  • "Build master" making sure we are able to build the game

We decided that we are going to build for Android, since all in our group have Android phones, and what we heard it's easier to build for it then iPhone iOS.

To implement a Step Counter into a game could of the bat sound trivial.
But as one as learned with programming, nothing is simple...

So first I justed tried to do a simple "get the accelerometer" from the phone, hence just adding steps when moving the phone up and down. And that worked, but it was super basic and the accuracy was extremely low. And the other caveat was that when you close down the app, the steps you take doesn't increase, which is game breaking.

I then looked into how to actually use the Android Sensor API, and as usally this sounded easy, you just have to create a plugin for Android and add it to Unity, and then it's done! Yeah right, not so simple...

So in order to create a plugin for Android, you first have to create a Java class, do an actuall compiled working app in Android Studio in order for to produce a plugin file, and .aar file. And that itself is not a simple task. I ended up compiling through Git Bash CLI style since I got compiling errors when I tried to do this in Android Studio for various reasons.

Once I was able to create the .aar plugin in file, I thought, yeeeaaaay now the hard part is done right? Nope. 
Next step was then to actually make Unity build with the plugin, and it was then I learned about Gradle, which seems to be famous making it hard for developers to compile the game. I ran into so many dependency problems, and I didn't even know where to start.
But after many days of trying to solve it, I finally made it to compile, and it feel like magic when I started the app and I got this:

I was blown away that it actually work, and the game was taping into the Android API in the right way. Now even when the app was in the background Steps where still incrementing in the background, and got updated once you opened up the app again.

This was a really big milestone for the whole group project, since it's way more accruate, it's actually uses the built-in sensor in the hardware of the phone.
It works in the background when the app is in idle mode.

Back-end

Since the course was also a Back-end course, we needed to create a Login/Authentication and store all the player data in a database.
We were tasked to use Google Firebase because it's free and easy to get going, you don't need to host anything yourself.

First we created a simple Authentication that worked, so you simply put in your e-mail and a password, and the register, we saw then that a User was created in Firebase Auth.

Now to the hard part. Storing the player data.
This was much trickier, since it's not only storing the data, we needed to keep in mind when to upload, and when to download the data. Since there is data stored locally on the phone.
It was at this time I learned about race conditions. To get everything to upload was fine, but what's the point of just uploading it? The whole idea is that we want store the players data so if the lose their phone/delete the app, they can retrieve all there precious progression.

At was at this stage I had to understand when and where the upload and the download should happen, and in what order, because the main problem from the start was that PlayerPrefs took priotiery and it wrote over the data we wanted to upload.
And I knew from the start, that we absolutely did not want the upload to happen in Update, since will hit our Write Limit of 50,000 per day really fast. 

So what I did was to create an event system and build ontop of that, and then have that we upload every 10 seconds if there had been a change in the steps. So if you just hold the phone, it will not upload until you actually have taken a step.

I can go on and talk about more stuff, but this was my main tasks, and at the end of the project I did about 20 different scripts in the game. 

We actually got so far in the project that we were able to release it on Google Play!

Link to Google Play Store