- Home /
Question by
ilya_ca · Aug 24, 2012 at 03:22 AM ·
iosaccelerometer
Accelerometer: How to make the camera always parallel to the floor
Hi, I'm trying to use accelerometer to have my camera always be parallel to the floor (something similar to the camera in Asphalt 7 Heat). Basically I want my camera to roll together with my iPhone to always keep it parallel to the floor. I'm using transform.LookAt( target, upVector) to orient my camera. The problem is with calculating the upVector. I thought that projecting the Acceleration vector onto my camera plane would solve the problem, but it won't do the trick.
Here's my update code for camera script:
void LateUpdate() {
Vector3 acceleration = Input.Acceleration;
Plane screenPlane = new Plane( transform.forward.normalized, transform.position );
Vector3 gravityPoint = transform.position + acceleration;
float dist = Vector3.Dot(screenPlane.normal, acceleration);
Vector3 projectedGravity = acceleration - screenPlane.normal * dist;
projectedGravity.Normalize();
transform.LookAt( targetPosition, -projectedGravity.normalized);
}
What's wrong with my idea of getting it to work? Thanks!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613080107im_/https://answers.unity.com/themes/thub/images/avi.jpg)