In January 2015, two lifelong friends started to collect people around their dream and LEAF Games & Software came to light. We are an indie Italian studio creating and developing mobile apps and PC games. All that we offer is meant to be innovative and it’s created from a scratch by a fresh and young development team. We believe in originality and uniqueness of ideas and we endeavor to promote the individual initiative enhancing the talent of each member of our team. We believe in people who dream big because LEAF people dream big. We want to offer unique products and grant a fun gaming experience to people of all ages. Our free to play apps are already available on Google Play Store and Apple App Store, and we are working on Leap of Champions, our first PC game. We ensure fun and excitement for the gamers, exactly what we demand for ourselves!

Post tutorial Report RSS How to implement the mobile accelerometer input in Unity5

Do you know Gameloft Asphalt8, one of the most popular video games which allows you to steer the car just moving the phone? Flight Simulator, Need for Speed Shift, Cube Runner? Ring any bells? With this post, we will show you how to use the smartphone motion detection mechanism in a video game implementing accelerometer data in Unity 5. Don’t’ worry, it's a simple script.

Posted by on - Basic Client Side Coding

What the accelerometer is?

The accelerometer is, in a nutshell, a chip installed in your smartphone which detects all the phone movements.

How to implement accelerometer data in Unity 5?

We can get the data through Input.acceleration

Code

Here, to use Input.acceleration.x and Input.acceleration.y was enough to obtain a movement on the transform.position.

It’s easy, doesn’t it?

However, implement motion through input.acceleration, is not always recommended. In many cases, in fact, we will have a GameObject moving crazy and without logic, so it’s better transform the data before rotate the GameObject.

Process the accelerometer data before rotate the GameObject.

This is an example:

Code

As you can see, I Lerped and adjusted input.acceleration values before using them, and I've got the desired result.

More information about accelerometer input are available here on the official Unity Learn section

I hope this tip has been helpful and that you can efficiently use the accelerometer to create your own video games in Unity 5!

Riccardo

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: