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 /
  • Help Room /
avatar image
1
Question by amiunity1 · Oct 07, 2015 at 04:02 PM · androidrotationaccelerometertilttilting

Rotate Object by Tilting Android Device Any Angle

I want to rotate an object (cube for example) all angles by tilting the Android device. I tried various code snippets online and none of them seem to work as needed. Can someone help by providing code that will do this properly?

Comment
Add comment · Show 2
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 amiunity1 · Nov 11, 2015 at 05:42 PM 0
Share

I have tried various combination of the scripts below and still did not succeed in doing this. All I need is an object to move together with the Android tilting movement. $$anonymous$$aybe someone can help alter one of the scripts below to make it work. I wrote exactly what is wrong so I believe there is someone out there that can easily fix one of the scripts below to work.

avatar image amiunity1 · Jan 27, 2016 at 06:23 PM 0
Share

I decided to go with the Gyroscope and try to figure out how to make the game available only to devices that have Gyroscope. But there is still one problem with the script. In order for it to work well, the mobile device has to be facing North. Can someone help me figure out what to change in the code to make it work regardless of what direction the mobile device is facing?

maybe @$$anonymous$$wahuNashoba can help (who provided the initial Gyroscope code)?

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by KwahuNashoba · Oct 12, 2015 at 04:33 PM

You'll need to enable gyro just in case first:

 void Start () {
 
         gyro = Input.gyro;
         if(!gyro.enabled)
         {
             gyro.enabled = true;
         }
     }

then:

 void Update () 
 {
       gameObject.transform.rotation  = gyro.attitude;
 }

That's it. After this you will have your object rotating based on phone orientation in space. You will probably have to do a lot of tweaks based on what you want to accomplish in your game/app.

Comment
Add comment · Show 7 · 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 amiunity1 · Oct 12, 2015 at 05:38 PM 0
Share

@$$anonymous$$wahuNashoba Thank you for the help :)

But, I couldn't get it to work. The object is not moving at all. Any ideas?

It looks like for Android people use Input.acceleration ins$$anonymous$$d of input.gyro. what do you think? http://answers.unity3d.com/questions/254111/how-to-enable-the-gyroscope-for-input-on-an-androi.html

I attached the script to the object that I want to rotate and changed the script so that there won't be any compile errors, so this is the exact script:

using UnityEngine; using System.Collections;

 public class AndroidInputControllerC : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {

         if(!Input.gyro.enabled)
         {
             Input.gyro.enabled = true;
         }
     }
     
     // Update is called once per frame
     void Update () 
     {
         transform.rotation  = Input.gyro.attitude;
     }
 }

avatar image KwahuNashoba amiunity1 · Oct 13, 2015 at 09:23 AM 0
Share

I accidentally removed my comment, could you paste it here from your mail? :D

avatar image KwahuNashoba amiunity1 · Oct 13, 2015 at 02:11 PM 0
Share

O$$anonymous$$, I'll just write it again :)

When I copy/paste this code to script and attach a script to the cube that you get when you create new scene it worked. Accelerometer can not be used for rotation (at least, it's not suppoused to) cause it represents phone acceleration in x, y and z direction and not orientation. That is why you get that strange behaviour and object is rotating even when you don't change phone orientation. No matter how much phone stands still, accelerometer will always give some value, even when it stands on the desk.

Ins$$anonymous$$d, find out value of transform.rotation after you call transform.rotation = Input.gyro.attitude; If it's something like (1,0,0,0) that means that gyro is not enabled for some reason and that's why your object do not rotate. A had problem few times with that.

Here is more detailed explanation of how to rotate object via gyro http://blog.heyworks.com/how-to-write-gyroscope-controller-with-unity3d/ . But if your object is not rorating at all it will not help.

avatar image amiunity1 KwahuNashoba · Oct 14, 2015 at 05:11 PM 0
Share

@$$anonymous$$wahuNashoba I really appreciate your help. Hopefully I'll get a really fun and original game out to the market soon.

I just checked another Android device that I have at home and it worked on that device (still with problems thought). The main problem though that it seems like using a gyroscope will cause the app not to be compatible with many Android devices. Is this true? It looks like my tablet Samsung GT-P5100 does have a gyroscope in it's specifications but the gyroscope script isn't working on it.

So, my question is, can the script be changed to support it, or is it better to use the accelerometer. According to developer.android.com, "$$anonymous$$ost Android-powered devices have an accelerometer, and many now include a gyroscope.".

Regarding the accelerometer giving a value even when standing on a desk, two of the three scripts I posted (#2 and #3) don't have the problem of getting a value all the time from the accelerometer. Each of these scripts has different issues. Only script #1 that I posted has the sensitivity issue.

Regarding the other problem the gyroscope script still seems to have, is that when rotating left/right or up/down, it moves the object diagonal (the object does not seem to be moving on the same axis as the Android device is moving).

avatar image amiunity1 · Oct 13, 2015 at 07:19 AM 1
Share

-the continuation of the previous post because of character limit-

3 . The following code works but has two issues. a) it is too sensitive. the object is constantly shaking and when rotating the object it rotates really shaky. b) when tilting the Android device from parallel-to-ground-face-up state to parallel-to-ground-face-down state, the object makes a whole rotation of 360 degrees, while it should be rotating only 180 degrees like the Android device. Another example: when tilting the Android device from parallel-to-ground-face-up state to horizontal state (when it's facing your face), the object turns 180 degrees ins$$anonymous$$d of 90 degrees.

 var rotx : float = 0;
 var roty : float = 0;
 var rotz : float = 0;
 var rotw : float = 0;
 
 function Start() {
     Screen.orientation = ScreenOrientation.LandscapeLeft;
 }
 
 function Update () {
     rotx = Input.acceleration.x;
     roty = Input.acceleration.y;
     rotz = Input.acceleration.z;
     transform.rotation = new Quaternion(rotx, roty, rotz, rotw);
 }
avatar image amiunity1 · Oct 13, 2015 at 11:52 AM 0
Share

Below are three different code-snippets I have tried that all work in different ways but each have different problems. $$anonymous$$aybe this can help someone to solve it. If anyone find a solution using this info, please post the solution because I'm stuck on this for a long time already.

  1. This following code works but has two problems.

a) It moved constantly ins$$anonymous$$d of stopping the object when the android is stopped. What is needed is that when the Android device is tilted left, the object will tilt left and then stop when the Android device is stopped. But the way this code works is that the object only stops moving when the Android device is help face up parallel to the ground.

b) When the object is tilted a certain direction then it changes the other directions tilting on. So if tilting on X axis then Y axis will title from where the object has been rotated so it actually won't be rotating on Y anymore. So ins$$anonymous$$d of tilting Y and the object rotating on Y, the object will no longer rotate on Y because the direction of the object has changed. What is needed that the object rotates the direction the Android device is tilted regardless of which direction the object is pointing because of previous rotation.

     var speed = 10.0;
     
     function Start() {
         Screen.orientation = ScreenOrientation.LandscapeLeft;
     }
     
     function Update () {
         var dir : Vector3 = Vector3.zero;
     
         // we assume that device is held parallel to the ground
         // and Home button is in the right hand
         
         // remap device acceleration axis to game coordinates:
         //  1) XY plane of the device is mapped onto XZ plane
         //  2) rotated 90 degrees around Y axis
         dir.y = -Input.acceleration.x;
         dir.z = Input.acceleration.y;
         
         // clamp acceleration vector to unit sphere
         if (dir.sqr$$anonymous$$agnitude > 1)
             dir.Normalize();
         
         // $$anonymous$$ake it move 10 meters per second ins$$anonymous$$d of 10 meters per frame...
         dir *= Time.deltaTime;
             
         transform.Rotate (dir * speed);
     }

The other two code snippets are in the next two comment because of character limit of the post.

avatar image amiunity1 · Oct 13, 2015 at 11:52 AM 0
Share

-the continuation of the previous post because of character limit-

2 . The following code works but has one problem: When the Android device is held face up parallel to the ground, when tilting the Android device left/right, the object rotates with a diagonal shift ins$$anonymous$$d of just rotating right/left. Also, when holding the Android device upside-down facing your face then it doesn't rotate the object well. This code works well only when the Android device is facing your face (not when help up parallel to ground or when held upside down facing your face).

     private var sizeFilter: int = 15;
     private var filter: Vector3[];
     private var filterSum = Vector3.zero;
     private var posFilter: int = 0;
     private var qSamples: int = 0;
     
     function Start() {
         Screen.orientation = ScreenOrientation.LandscapeLeft;
     }
     
     function $$anonymous$$ovAverage(sample: Vector3): Vector3 {
          if (qSamples==0) filter = new Vector3[sizeFilter];
          filterSum += sample - filter[posFilter];
          filter[posFilter++] = sample;
          if (posFilter > qSamples) qSamples = posFilter;
          posFilter = posFilter % sizeFilter;
          return filterSum / qSamples;
     }
     
     function Update () {
         transform.up = -$$anonymous$$ovAverage(Input.acceleration.normalized);
     }

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

40 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

Related Questions

Tilt object based on movement and direction 1 Answer

Gravity on Android 1 Answer

Tilt camera on movement 2 Answers

How to make platform to rotate player ? 0 Answers

Problem titling an object from its center (Pitch/Roll) (screenshot) 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