- Home /
Mapping accelerometer to Camera's position
This seems simple enough, but I am having some problems with it. I am trying to map the accelerometer to a first person camera. The goal is have the crosshairs (which i can just parent to the camera) move as the player tilts their device.
I understand how to access the accelerometer x, y, and z but I cannot figure out how to make the camera work with these numbers (if that is even possible)
Thanks
Answer by ilya_ca · Aug 24, 2012 at 04:28 AM
Try using Gyroscope. Something like this: camera.transform.rotation = Input.gyro.attitude; Or do you need just the up-down motion of the camera? If so, try using the following code in your camera script:
float hRotation = 0.0f; //Horizontal angle
float vRotation = 0.0f; //Vertical rotation angle of the camera
float cameraMovementSpeed = 0.02f;
void Update() {
hRotation -= Input.acceleration.y * cameraMovementSpeed;
vRotation += Input.acceleration.x * cameraMovementSpeed;
transform.rotation = Quaternion.Euler(vRotation, hRotation, 0.0f);
}
Answer by Martians135 · Jun 14, 2017 at 10:09 AM
It's Possible do it with accelerometer, you can rotate camera with the accelerometer. On all axes. With this, a virtual reality application works on all phones: https://www.assetstore.unity3d.com/en/#!/content/87609