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 Der_Kevin · Nov 20, 2015 at 10:39 AM · accelerometertiltgyro

input.acceleration - change the zero position of the phone?

Hey Friends, so i have this pice of Code: using UnityEngine; using System.Collections;

 public class TiltPlatform : MonoBehaviour {
     
        public bool smoothMotion = true;
     
     //This is how quickly we MoveTowards the input axis.
     public float smoothSpeed = 1f;
     
     //The maximum we want our input axis to reach. Setting this lower will rotate the platform less, and higher will be more.
     public float multiplier = 0.1f;
     
     //Variables, don't edit these.
     private float hSmooth = 0f;
     private float vSmooth = 0f;
     private float h;
     private float v;
     
     void Update () {
         //Get Vertical and Horizontal axis from Input
         //h = Input.GetAxis("Horizontal")*multiplier;
         //v = Input.GetAxis("Vertical")*multiplier;
         h = Input.acceleration.y*multiplier;
         v = Input.acceleration.x*multiplier;
         
     }
     
     void FixedUpdate() {
         hSmooth = Mathf.MoveTowards(hSmooth,h,smoothSpeed* Time.deltaTime);
         vSmooth = Mathf.MoveTowards(vSmooth,v,smoothSpeed * Time.deltaTime);
         
         //Rotate to match the new axis using EulerAngles.
         if(!smoothMotion){
             transform.rotation = Quaternion.EulerAngles(new Vector3(h,0f,v));
         }else{
             transform.rotation = Quaternion.EulerAngles(new Vector3(hSmooth,0f,vSmooth));
         }
     }
 }

which causes this: https://d13yacurqjgara.cloudfront.net/users/76292/screenshots/2205018/tiltplatform.gif

which is great so far, no problem at all. but at the moment i have to lay my phone straight to the horizon so that the platform is not moving in any directions (0°) what i want to achieve is that when you tilt your phone... lets say 45° the platform should be straight. and then when you place it straight to the horizon the platform should tilt back. here is a small sketch for better understanding: alt text

i think the most important line for from the script is this:

 void Update () {
         //Get Vertical and Horizontal axis from Input
         //h = Input.GetAxis("Horizontal")*multiplier;
         //v = Input.GetAxis("Vertical")*multiplier;
         h = Input.acceleration.y*multiplier;
         v = Input.acceleration.x*multiplier;
         
     }
     
     void FixedUpdate() {
         hSmooth = Mathf.MoveTowards(hSmooth,h,smoothSpeed* Time.deltaTime);
         vSmooth = Mathf.MoveTowards(vSmooth,v,smoothSpeed * Time.deltaTime);
         
         //Rotate to match the new axis using EulerAngles.
         if(!smoothMotion){
             transform.rotation = Quaternion.EulerAngles(new Vector3(h,0f,v));
         }else{
             transform.rotation = Quaternion.EulerAngles(new Vector3(hSmooth,0f,vSmooth));
         }
     }

tilting.jpg (150.3 kB)
Comment
Add comment · Show 3
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
avatar image savlon · Nov 20, 2015 at 01:52 PM 0
Share

I'm not sure if you can change the zeroing out part of the acceleromer but you could just store some floats and apply them every update. Like say you have a screen where the user changes the angle on their phone to be the zero'd out angle. Store those values and apply them each interation to be its "new" zero'd position. Then just apply their movements on top of that? Never tried it but just throwing out ideas.

avatar image Der_Kevin savlon · Nov 20, 2015 at 02:06 PM 0
Share

hm. so actually when
v = Input.acceleration.x*multiplier;

then i just have to say on update vzero = v - 45 (i actually dont know how the tilting float looks like when i hold the phone 45°.. 0.45, 45, 0,0045? and then

vSmooth = $$anonymous$$athf.$$anonymous$$oveTowards(vSmooth,vzero,smoothSpeed * Time.deltaTime);

right?

avatar image savlon Der_Kevin · Nov 21, 2015 at 12:08 AM 0
Share

I don't believe it calculates it in degrees. I'm not entirely sure what value you should put in but you could Debug.Log the base acceleration and then move it into your desired value. Then with the desired values, use them to zero it out. For more of an idea of what I mean, check this out http://answers.unity3d.com/questions/663283/how-to-calibrate-my-accelerometer-data.html . This person obtains the accelerometer values when the script Start()'s and then applies them to calibrate it. FYI - I'm talking about the answer to the question, not the question itself.

0 Replies

· Add your reply
  • Sort: 

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

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create tilt control similar to temple run? 0 Answers

Detect car acceleration with device motion sensors and GPS 0 Answers

Move if mobile tilts in positive Y direction 0 Answers

Desperate for help on control for a ball on android phone. 1 Answer

iPad: accelerator tilt breaks collider 3 Answers


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