Today’s project from Matt is one of those that you might not use or do for your day job, but is something awesome to do on the weekends!

FlappyHand: Unity3D game with an ESP8266 Arduino (Wemos) controller over WiFi

I thought it would be fun to make a little FlappyBird clone using an ultrasonic sensor as the controller.
So I threw this together yesterday:


What you’re seeing there is an ultrasonic sensor on the table reading how far my hand is away from it, then it sends that value over WiFi to the Unity3D game, which then maps that to the plane movements.

There is a bit of latency between moving your hand and the plane moving, but from what I can tell that is more caused by actually reading from the ultrasonic sensor than network latency


All the code is on GitHub, go grab it!

WiFi Controller

The controller bit is pretty simple, and is a WiFi-connected Arduino with a ultrasonic sensor that sends sensor readings over the network to the Unity game.

I’m using an awesome little device called a Wemos, which is basically a fancy breakout board with an ESP8266 WiFi module that you can load Arduino onto.

Check it out here, and buy it from their official store on AliExpress here for $4.

The only other thing you need is an ultrasonic sensor module. I’m using the HC-SR04, which you can get for less than $1.

We’ll be sending the sensor values over a TCP connection to the client (i.e. the game). The Arduino code connects to a WiFi network, then waits for a client. Once a client connects to it constantly measures the value from the ultrasonic sensor, then sends it to the client. It’ll do this over and over again until the client disconnects.

We’re using a carriage return (“r”) as a termination between each reading (else the client wouldn’t know where one reading ends and the next one starts.


The Game

I should start off by saying that this is probably not a brilliant example of how to put a game together. It was done in a couple hours and was simply to test this concept, not to be an actual game.

So yeah, it’s a FlappyBird clone. Because there aren’t enough of those.

The art is from the very awesome Kenny, who has tons of great stuff (and lots of it is free if money isn’t your thing).

I’m only going to go over the more interesting stuff, so if you want to see how the game is put together checkout the GitHub link at the top. All the scripts are nicely commented.

The main network stuff is this little bit of code:

That’ll just constantly read from the network and remap the received sensor values into the Y movement range of our plane.

Since both the background, and the terrain is a tiled sprite, we need some code to tile it all. The code below works out when it needs to make a new tile by looking where the camera is. So as the camera moves across, it’ll keep on generating new tiles as needed. In a real-world you’d probably want to remove unneeded tiles once they go off-screen too.

[Click through to see the code and more…]

Source link