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.
- 1 Features of Sensor Camera
- 2 Package Contains
- 3 Gyro vs. Accelerometer
- 4 Limitations
- 5 Use Cases of Sensor Camera
- 6 Tutorial
- 7 Testing
- 8 Support
- 9 Changelog
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 (Android).
- 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.
- 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.
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.
- 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
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 tutorial is relevant for Sensor Camera 2.0+.
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.
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.
- Create a New Unity Project with Unity 2022.3.6 & “3D” Template.
- File > Build Settings > iOS, Android or WebGL > Switch Platform.
- Next Packages must be manually installed with Unity Package Manager:
- Input System 1.6.3.
- 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.
- Input System 1.6.3.
- Download and Import Sensor Camera into Unity.
- Window > TextMeshPro > Import TMP Essential Resources.
- Reopen Unity Project.
- Open Scene: Makaka Games > Camera > Sensor Camera > Scenes > Demo.
- File > Build Settings > Add Scenes to build:
- LoadScreen (from Makaka Games > Publisher > Everyday Tools > SceneControl > Scenes),
- 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.
- Test in the Unity Editor or Build for Mobile.
Useful Article: How to Test iOS App without Developer Account?
SensorCamera prefab has a main camera control script: SensorCameraControl.cs.
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)
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.
On Initialized in Editor
The case when Play Mode in Unity Editor.
On Initialized Not in Editor
The case when Runtime on Device.
Since the accelerometer cannot provide the same camera rotation natural experience as a Gyroscope, you can adjust the Accelerometer Sensitivity required for your game.
There are 2 ways of testing without building an app:
- Testing with Unity Remote & Smartphone connected to Computer.
- Testing without a Smartphone in Unity Editor.
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.
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.
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.
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:
- Right Above the Ring;
- 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.
Learn the Article called WebGL and Unity about Building and Testing Unity games and apps for WebGL.
Tested with Platforms
- Mobile Platforms with Motion Sensors:
- iOS on iPhone 8, XS Max;
- Android on Samsung Galaxy A71;
- WebGL (Android) in Google Chrome.
- Desktop Platforms with Right Mouse Button:
- WebGL in Google Chrome.
Check the current version of Sensor Camera on Asset Store.
The latest versions will be added as soon as possible.
2.0 (WebGL Support):
- WebGL Support via HTTPS: Android.
- Check information about iOS in the Limitations section.
- Camera Y Rotation Sensitivity with Accelerometer for Different FPS.
1.1 (New Settings of Accelerometer – can be set in Unity Editor):
- Checkboxes for Testing:
- Is Accelerometer Unsupported Not In Editor Test;
- Is Accelerometer Supported In Editor Test.
- On Accelerometer Initialized;
- On Accelerometer Is Not Supported.