Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MomijiStudios · Mar 21, 2014 at 03:52 PM · controlaccelerometeraccelerationmotionupside-down

Recalibrated 'zero' point for Input.acceleration messes up when goes to upside-down

My issue is a little hard to explain, but I'll try. In my game you control a ball by tilting the phone. Simple code. The problem comes with the recalibration option in the game.

If Recalibration Mode is enabled, the player holds the phone in the position they want to be the 'flat' position, then touches the screen to start. Doing that calls this code:

 public void InitializeAxis () {
     offsetX = Input.acceleration.x;
     offsetY = Input.acceleration.y;
     offsetZ = Input.acceleration.z;
 }

Basically, the calculations that normally go into adding force to the ball simply subtract the offset value. This makes it so any position can be the 'flat' position, and all of the movements are based on the relativity to that position.

It works great, but there is a HUGE flaw with it. I've been working on it nonstop for over 5 hours today, and I've pretty much given up. This is the problem: once the phone is 'flipped' into an upside down position (basically indicated by the Z value becoming positive instead of negative), ball flies off the stage erratically.

I thought I had it figured out, because the X and Y accelerometer values basically get to 1 or -1 once it reaches it's peak, then starts counting back to zero. I somewhat figured out how to solve that problem...at least I thought I did. Here's what my code looks like for when the phone was started screen-side-up:

 else {  // phone started screen side up (z was negative)
                     if (Input.acceleration.z <= 0) {  // if it's still screen side up, process normally
                         x = Input.acceleration.x - offsetX;
                         y = Input.acceleration.y - offsetY;
                         movement = new Vector3(x, 0.0f, y);
                     }
                     else {  // now it's screen side down (z is positive now)
                         if (Input.acceleration.x >= 0) {
                             x = (2 - Input.acceleration.x) - offsetX;
                         }
                         else {
                             x = (-2 - Input.acceleration.x) - offsetX;
                         }
                         
                         if (Input.acceleration.y >= 0) {
                             y = (2 - Input.acceleration.y) - offsetY;
                         }
                         else {
                             y = (-2 - Input.acceleration.y) - offsetY;
                         }
 
                         movement = new Vector3(x, 0.0f, y);
                     }
                 }
                 rigidbody.AddForce(movement * speed * Time.deltaTime);
             }


Here's the big problem: say, for example I tilt the top of the phone upward towards me (Y axis going down, ball rolling down on the screen). Once it reaches the point where it's facing directly toward me, phone 'standing' upright, and I pass that threshold where the phone has now become upside-down (Z axis has become positive instead of negative), the Y value is fine. Because of the code, it basically just adds how far it's moved past that threshold to the value.

But then, the X value just gets incredibly close to 2, flailing the ball of the stage. It's almost like I need some way to tell which axis actually pushed it over that threshold. If I could do that, I could solve it (say the Y made it flip over; I could just do: x = Input.acceleration.x * -1; and everything would be fine).

I feel like there is some incredibly simple solution to this, and I'm just overthinking it. Or maybe it really is a problem, and I need a mathmegician to figure it out for me.

I know this was a long explanation, but it's 5:30 a.m. and I am incredibly frustrated and exhausted after a night of trying to solve this one silly problem. Ask any questions you might have, or tell me if you need any information. Just please help me!

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

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

21 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Input.acceleration uses gravity sensor 0 Answers

Acceleration which unit 0 Answers

Building a speed limit and deceleration system, need help fixing/optimising 0 Answers

Detect phone sliding movement across table 0 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