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
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.

Tutorial
Browse
Tutorials
Report
Report
Share
Related Groups
Android Fans
Android Fans Hardware & Tech with 485 members
LEAF games & software
LEAF games & software Developer with 2 members
Unity Games
Unity Games Hobbies & Interests with 1,771 members
Got it!

We have recently updated our privacy policy and terms of use in-line with GDPR requirements. More Info?