As an Associate of Unity Asset Store, this website earns from qualifying purchases & contains affiliate links: check the footer for more info.

Sensor Camera: Gyroscope & Accelerometer — Unity Asset: FPS Camera with Motion Sensors

Sensor Camera — Unity Asset that uses a Motion Sensor (Gyroscope or Accelerometer) on the player’s mobile device to control the game camera from a first-person view (just like in FPS Games). Safety First.

Unity Asset Store — Download Button

It doesn’t use live video input from the Back/Rear Camera of a Mobile Device like in
AR Camera Lite (docs) or in AR + VR: MR Camera (docs).

Features of Sensor Camera

Bring the enchanting Power of Mobile Sensors into your amazing Game or App:

  • Cross-platform First-Person Camera controlled by Motion Sensors:
    • iOS, Android, WebGL.
  • Auto Selection of Sensor: Gyroscope or Accelerometer.
  • Auto Rotation: Portrait, Landscape.
  • Gyroscope Mode: 3DoF — it can track rotational motion, but not translational.
  • Accelerometer Mode (horizontal rotation is limited): tilting the phone to the left or right rotates the camera around the Y-axis.
  • Quick Testing in Unity Editor without a Phone through the Right Mouse Button & WASDQE keys.

Package Contains

  • Demo Scene with Cubes.
  • Menu Scene with Safety Tutorial.
  • Loading Screen to switch scenes seamlessly.

Check the Map of Unity Assets to choose the product that best suits your needs.

Gyro vs. Accelerometer

90% of all mobile devices have an accelerometer and video camera, but only 40% have a gyroscope.

If the user’s smartphone has a Gyroscope, then it will be used for camera motion first. Otherwise, an Accelerometer will be used because it has less accuracy & stability than a Gyroscope in the case of the First-Person View.


Limitations

Hardware nuances of the gyroscope & accelerometer (asset code does not affect it):

  • Different devices have different sensors, and therefore different deviations and drifts.
  • Drift is natural for the mobile sensors.

Known Issues

  • Fixed in Unity 2022.3.18: Orientation Changing on WebGL (iOS) — Bug in Unity Engine: when you change device orientation during the game, then it is not defined correctly sometimes. Solution: warn the player about making correct orientation before starting of Unity Project on a new tab.
  • Hardware Issue of Gyroscope on Motorola Moto G4 and G5:
    INPUT.GYRO.ATTITUDE returns zero values.

Pro Gamer Tip

Accelerometer & Gyroscope are used in games and apps to control gameplay like in PUBG MOBILE game. Sometimes these sensors can be set up incorrectly for some reason & break the gameplay. If you guess that your drift of gyro or accelerometer is not normal, then try to calibrate them with system tools provided by your smartphone manufacturer.

Use Cases of Sensor Camera

Basketball Game 3D (docs).


Football Game 3D (docs).


With adding live video input from the Back/Rear Camera of a Mobile Device, Sensor Camera can be used as a “Pseudo AR Camera” to display 2D or 3D objects as though they were in the real world.

This technique was implemented in AR Camera Lite (docs).


AR Shooter (docs).


AR Basketball (docs).


AR Throw & Score (docs).


AR Throwing (docs).

Tutorial

This tutorial is relevant for Sensor Camera 2.1+.
Tutorial for the previous version can be found only in the asset folder.

Getting Started with Sensor Camera

Folders & Files in the package by default:

  • Makaka Games.

Steps

If you have any issues with the first launch, then just Reach Support with Invoice Number and Get Help.

If you read this tutorial from PDF, first check the latest docs online to get actual information.

  1. Create a New Unity Project with Unity 2022.3.18 & “3D” Template.
  2. File > Build Settings > iOS, Android or WebGL > Switch Platform.
  3. Next Packages must be manually installed with Unity Package Manager:
    • Input System 1.7.0.
      • Warning Window: Click “Yes” when enabling the backends for the new input system. After installing, you will have the next value in settings: Edit > Project Settings > Player > Other Settings > Configuration > Active Input Handling > Both.
  4. Download and Import Sensor Camera into Unity.
  5. Window > TextMeshPro > Import TMP Essential Resources.
  6. Reopen Unity Project.
  7. Open Scene: Makaka Games > Camera > Sensor Camera > Scenes > Demo.
  8. File > Build Settings > Add Scenes to build:
    • Menu,
    • LoadScreen (from Makaka Games > Publisher > Everyday Tools > SceneControl > Scenes),
    • Demo.
  9. Option: Limit the Screen Orientation from Auto (by default) to the Portrait only or Landscape only by indication of appropriate Scene Loading Methods on the corresponding buttons that cause a transition to another scenes. Orientation in the Player Settings is used for the 1st scene in the build.
  10. Test in the Unity Editor or Build for Mobile.

Useful Article: How to Test iOS App without Developer Account?

Script Manual

SensorCamera prefab has a main camera control script: SensorCameraControl.cs.

Sensor Camera: Gyroscope & Accelerometer — Unity Asset: FPS Camera with Motion Sensors
Sensor Camera: Gyroscope & Accelerometer — Unity Asset: FPS Camera with Motion Sensors

Flag: Is Gyro Disabled On Destroy

If it’s “true”, then Gyro’s “Y” Rotation is reset on Scene Closing or Reloading. Useful if you need to Control the Start Rotation of the Camera when Restart.

Testing Not in Editor (Flags)

Is Gyro Unsupported Not In Editor Test

If it’s “true”, then the Gyroscope will be unavailable on Gyro supported devices.
It is used when you need to test the Accelerometer on Gyro supported devices.

Is Accelerometer Unsupported Not In Editor Test

If it’s “true”, then the Accelerometer will be unavailable on Accelerometer supported devices.

Testing in Editor (Flags)

Is Gyro Supported In Editor Test

If it’s “true”, then you can test Gyro-related Events in Unity Editor.

Is Accelerometer Supported In Editor Test

If it’s “true”, then you can test Accelerometer-related Events in Unity Editor.

Is Movement WASDQE In Editor Test

If it’s “true”, then you can fly in the scene when Play Mode in Unity Editor.
It’s used for different testing scenarios.

Events (in the order of execution)

Sensor Events
On Gyro Initialized

The case when the smartphone has the Gyroscope.

On Gyro Is Not Supported

The case when the smartphone has not the Gyroscope.

On Accelerometer Initialized

The case when the smartphone has the Accelerometer.

On Accelerometer Is Not Supported

The case when the smartphone has not the Accelerometer.

Common Events
On Initialized in Editor

The case when Play Mode in Unity Editor.

On Initialized Not in Editor

The case when Runtime on Device.

Accelerometer Settings

Since the accelerometer cannot provide the same camera rotation natural experience as a Gyroscope, you can adjust the Accelerometer Sensitivity required for your game.

Testing

There are 2 ways of testing without building an app:

Testing without the Smartphone in Unity Editor

You can test camera rotation quickly in Unity Editor without Smartphone through the Right Mouse Button.

Also, you can use WASDQE keys to move the camera.

Use Fullscreen of Game View in Unity Editor while testing to get a seamless experience.

Accelerometer Instead of Gyro

You can forcibly test Game Version with accelerometer on gyro supported devices:
Hierarchy View > SensorCamera > SensorCameraControl > Is Gyro Unsupported Not In Editor Test > Check, then Build And Run.

Predefined Data

Also, you can start the scene with Predefined Data of position & rotation. It’s a convenient way to frequently test the same positions and rotations of the camera: “Main Camera” Game Object.

Sensor Camera: Gyroscope & Accelerometer — Unity Asset: FPS Camera with Motion Sensors — Editor Testing

So, you can save these data with custom Transform components on Game Objects as shown on the screenshot: as children of “TestViewWithoutSensor” Game Object.

These transform components are parameters for the function called SetPositionAndRotation (Transform) which is executed on game start when “OnInitializedInEditor” event is called.

Use Case

I used this testing method when developing AR Basketball (docs). I needed to periodically test Normal Ball (with touching of Ring) & Clear Ball (without touching of Ring). Since throw “Clear Ball” is a hard task, I saved 2 different camera Transform components to change them when needed:

  1. Right Above the Ring;
  2. A Few Meters from the Ring.

Testing Time was decreased well because I didn’t need to take the mobile phone every time in my hands after changes in the game logic and move the phone manually. Instead of it, I had predefined data.

WebGL

Learn the Article called WebGL and Unity about Building and Testing Unity games and apps for WebGL.

Tested with Platforms

  1. Mobile Platforms with Motion Sensors:
    • iOS on iPhone 8, XS Max;
    • Android on Samsung Galaxy A71;
    • WebGL in Google Chrome.
  2. Desktop Platforms with Right Mouse Button:
    • Windows;
    • macOS;
    • WebGL in Google Chrome.

Support

First, read the latest docs online.
If it didn’t help, get the support.

Changelog

Check the current version of Sensor Camera on Asset Store.
The latest versions will be added as soon as possible.

2.1:

Features:

  • iOS WebGL Support via HTTPS (the bug was fixed by Unity without confirmation).

Improvements:

2.0 (WebGL Support):

Features:

  • WebGL Support via HTTPS: Android.
    • Check information about iOS in the Limitations section.

Improvements:

1.2:

Fixes:

  • Camera Y Rotation Sensitivity with Accelerometer for Different FPS.

1.1 (New Settings of Accelerometer – can be set in Unity Editor):

Features:

  • Checkboxes for Testing:
    • Is Accelerometer Unsupported Not In Editor Test;
    • Is Accelerometer Supported In Editor Test.
  • Events:
    • On Accelerometer Initialized;
    • On Accelerometer Is Not Supported.

Improvements:

Unity Assets

Support for Unity Assets

I am Andrey Sirota, Founder of Makaka Games and full-time Publisher on the Unity Asset Store. First, read the latest docs online. If it didn’t help, get the support.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment moderation is enabled. Your comment may take some time to appear.

Back to top button