• 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 /
  • Help Room /
avatar image
0
Question by Alexsutton · May 27, 2016 at 01:18 PM · scripting problemaccelerometer

Accelerometer to rotate an object to a specific angle within a range

I'm working on a game for Android where you tilt the device side to side to control gravity and affect the path/route of falling objects with physics.

I have set up my game 'board' with a fixed camera so rotating the whole object appears to 'shift gravity' from the players perspective.

I want the board to tilt no more than about 30 degrees either way around the X axis however at the moment my code isn't doing what I want. I'm using transform.up to change the rotation of the board according to the accelerometer.

 void FixedUpdate () {
         Vector3 dir = Vector3.zero;
         dir = Input.acceleration.normalized;
         dir.y = 0;
         dir.z = 0;
         if (dir.x >= 0.3) {
             dir.x = 0.3f;
         } else if (dir.x <= -0.3) {
             dir.x = -0.3f;
         }
         Debug.Log (dir);
         transform.up = dir.normalized;
     }

Reading the Log output in gameplay shows the Vector3 variable 'dir' exactly as I expect it to. If I tilt my device to one side the value gradually increases to 0.3 (or -0.3) and then stays at that value even if I rotate well past it.

The problem is though, currently the board I am rotating just snaps from 90 degrees on it's left side to 90 degrees on it's right side, with no range of movement anywhere between the two. It just jumps from one orientation to the other depending on which side of upright the device is tilted.

I'm pretty new to scripting so I may well be missing something obvious. I have had it working how I want it to previously (although without the range limiting) but I can't see any singificant changes in the code as to why it no longer rotates evenly along with the device.

Any help/suggestions would be much appreciated!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Alexsutton · May 29, 2016 at 08:22 PM

Solved the issue, the problem was caused by setting both the Y and Z axis to 0, as that then means the vector can only point in one of two directions, which then manifested itself as my object 'snapping' between those two directions. I solved the issue by setting the Y axis to 0.3f and this has produced the correct behaviour, although I still don't entirely understand why that doesn't mean my object isn't slightly angled along that Y axis.

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
avatar image
0

Answer by winster287 · Mar 08, 2020 at 05:23 PM

I know this is a slightly old post but I see a clear mistake in the code because of which you ain't getting the desired rotation. Since the values are switching between the two sides of zero (-0.3 to 0.3), there need to be two more conditions for when the value should not directly snap to the extreme values (i.e when the accelerometer output is between the max and max range).

void FixedUpdate () { Vector3 dir = Vector3.zero; dir = Input.acceleration.normalized; dir.y = 0; dir.z = 0;

      if (dir.x < 0.3 && dir.x>0) {
          FinalOutputX = dir.x; } 

      else if (dir.x > -0.3 && dir.x < 0) {
          FinalOutputX = dir.x; }

      else if( dir.x > 0.3 ) {
          FinalOutputX = 0.3;}

      else if( dir.x < -0.3 ) {
          FinalOutputX = -0.3;}
          Debug.Log (dir);

}

The first two statements could be made into one but I prefer having them as two statements so that some things can be tweaked further. However, I haven't tested this code, but it should work alright. If you understand the logic, you'll be able to edit it as per your game. And this will solve the snapping issue you were having. @Alexsutton

Comment
Add comment · Show 1 · 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
avatar image Alexsutton · Mar 08, 2020 at 06:54 PM 0
Share

Thank you so much for your reply @winster287, however I don't think you are correct. Your first two if statements achieve the same goal as the last line of my original code, where if neither of the if statements are true then the script just uses the original, unaltered dir value.

The issue, I found, was because I had forced the X and Y values of dir to be zero, essentially creating a one dimensional vector that could only point either to the left or the right, or perfectly at zero.

Your code works because you've left out the statements forcing the x and y value of the vector to zero, if you add those back in I suspect you'll find the same bug.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

66 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 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 avatar image avatar image

Related Questions

How do i limit my accelerometer movement 1 Answer

Acceleration input math problem. 0 Answers

How to trigger animation? 0 Answers

Sun Rotation to Hour? 1 Answer

Making a pivot focused game 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