Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by beefyt123 · Sep 11, 2015 at 04:40 PM · rotationaccelerometertiltrotation detectioninput.acceleration

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.

alt text

unity-question.png (65.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges