I want to rotate camera vertically on accelerometer but device should be vertical to the ground not parallel…I wrote code that works fine if device is parallel to the ground what will be the code if device is perpendicular to the ground…
Code for rotation while device is parallel
The accelerometer axes are aligned as follows: Y aligns to the long side of the device, X aligns to the short side and Z is perpendicular to the screen. If you use acceleration.x, the camera will rotate when the device inclines from vertical to right or left. If you use acceleration.z, you must swing the device back and forth.
NOTE: never modify single components of eulerAngles! At certain angles the XYZ combination suddenly changes to weird values, screwing up the rotation. Save the initial eulerAngles in a Vector3, rotate the desired value and assign it back to eulerAngles every Update:
private var euler: Vector3;
function Start(){
euler = transform.localEulerAngles; // save the initial localEulerAngles
}
function Update(){
...
// lerp the angle saved, not localEulerAngles:
euler.x = Mathf.LerpAngle(euler.x, vertical, Time.deltaTime * 5);
transform.localEulerAngles = euler; // update actual rotation
...
}