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 xProNinja · May 07, 2014 at 12:20 PM · android2drotationphone

Rotating a 2D object when the phone is rotated

I am developing a 2D game. I want the character to rotate when the phone is tilted.

For example, if the phone is tilted left; the character would start rotating left.

How would I go about doing this?

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
0
Best Answer

Answer by tetigi · May 07, 2014 at 12:47 PM

You can get acceleration values by using Input.acceleration - based on the orientation of the phone, this will let you know how they are tilting it. You can find out more about that here.

Once you have worked out the orientation of the phone, you can rotate the character using the GameObject.transform.eulerAngles variable. This takes a Vector3, where the x/y/z is the rotation in the x/y/z axis.

So, if you just wanted to rotate your gameobject based on when they tilt left and right, your code might look something like:

 float const ROTATE_AMOUNT = 2; // at full tilt, rotate at 2 degrees per update
 
 float GetTiltValue() {
   float const TILT_MIN = 0.05f;
   float const TILT_MAX = 0.2f;
   
   // Work out magnitude of tilt
   float tilt = Mathf.Abs(Input.acceleration.x);
 
   // If not really tilted don't change anything
   if (tilt < TILT_MIN) {
     return 0;
   }
   float tiltScale = (tilt - TILT_MIN) / (TILT_MAX - TILT_MIN);
   
   // Change scale to be negative if accel was negative
   if (Input.acceleration.x < 0) {
     return -tiltScale;
   } else {
     return tiltScale;
   }
 
 }
 
 void Update() {
   float tiltValue = GetTiltValue();
   Vector3 oldAngles = this.transform.eulerAngles;
   this.transform.eulerAngles = new Vector3(oldAngles.x, oldAngles.y, oldAngles.z + (tiltValue * ROTATE_AMOUNT));
 }

This code should rotate the game object it is attached to at different speeds, relating to how tilted the device is. Let me know if you have any questions!

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

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 rotate 2d obj on android 1 Answer

Animated sprites disappear then rotated on Android 6 Answers

Random X posistion between left and right edges of screen 2D 1 Answer

2D mobile: Need my character to rotate Z axis towards touch position 0 Answers

How to control a 2d character in an top-down android game with the standard joystick? 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