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?
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.
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)?
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.
@$$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;
}
}
I accidentally removed my comment, could you paste it here from your mail? :D
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.
@$$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).
-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);
}
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.
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.
-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
Follow this Question
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