The art of Windows Phone

Menu

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.

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.

// If this wasn't the end of a reading, then just add this new byte to our buffer

buffer.Add((byte)read);

}

});

thread.Start();

}

}

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.