- Home /
Smooth tilt accelerometer
Can someone please help me. I have a tilt function that takes the accelerometer input from a mobile device to rotate my player which works but the rotation is really shakey. I have been doing research and everyone is pretty much saying to use Lerp instead of Euler but every time I have tried I have been unsuccessful in converting the script. Can someone please help me with converting from Euler to Lerp or give me suggestions on a better way of achieving my desired outcome.
     public float speed = 1f;
     private Vector3 myRotation;
     public Transform particle;
     
     void Start() {
         myRotation = gameObject.transform.rotation.eulerAngles;
 
     }
     
     void Update() {
 
         //float GetComponent<ParticleSystem>().GetComponent<Rigidbody2D>().transform.position.x
 
             if (Input.acceleration.normalized.x > 0) {
                 myRotation.z = Mathf.Clamp (myRotation.z - 1f, -60f, 45f);
                 transform.rotation = Quaternion.Euler (myRotation);
             }
             if (Input.acceleration.normalized.x < 0) {
                 myRotation.z = Mathf.Clamp (myRotation.z + 1, -60f, 45f);
                 transform.rotation = Quaternion.Euler (myRotation);
             }
 }
here is an a quick image I made up in paint to show what I want to happen.

Answer by raygun · Aug 29, 2016 at 05:25 PM
Here's my rotating code. Also note that it is better to use a ATan2 between the 2 points of the vector you need the angle for. This would depend on the orientation you're working with. Using ATan2 would give you an angle that would feel more steering-wheel-like.
 float _rollAngle;
 
 void Update()
 {
     if (rollObject != null) 
     {
         var angle = Mathf.Atan2(Input.acceleration.x, Input.acceleration.y) * Mathf.Rad2Deg;
         _rollAngle = Mathf.LerpAngle(_rollAngle, angle, 10 * Time.deltaTime);
         
         rollObject.transform.localRotation = Quaternion.AngleAxis (_rollAngle, Vector3.up);
     }
 }
The following answer worked for me:
http://answers.unity3d.com/questions/634675/how-to-smooth-accelerometer-values.html
Your answer
 
 
             Follow this Question
Related Questions
Smooth Accelerometer tilt rotation 0 Answers
Input.acceleration reading Issue 1 Answer
iPad: accelerator tilt breaks collider 3 Answers
Using the accelerometer as controller on android. 2 Answers
Anti-Gravity vehicle bounce-back? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                