- Home /
Gyroscope Controls for Android?
I'm just finished the rolling ball tutorial in the getting started guides:
http://unity3d.com/learn/tutorials/projects/roll-a-ball
I wanted to run it on my android device. I thought I'd try to use the gyroscope to control the ball motion, but I can't seem to find any good examples of this in code. Could anyone talk me through how that would go?
right now what I ahve looks like this:
void doMovement() {
float moveHorizontal;
float moveVertical;
if(Input.gyro.enabled) {
Gyroscope gyro = Input.gyro;
moveHorizontal = gyro.gravity.x;
moveVertical = gyro.gravity.y;
} else {
moveHorizontal = Input.GetAxis ("Horizontal");
moveVertical = Input.GetAxis ("Vertical");
}
Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
rigidbody.AddForce (movement*speed*Time.deltaTime);
}
But I don't really understand how to debug Unity on android, and I can't really tell whats happening (or if this is even the right way to approach the problem).
Answer by Aleksandar_Stefanovic · Oct 28, 2015 at 12:50 PM
this is the code I've used
void FixedUpdate()
{
Input.gyro.enabled = true;
float initialOrientationX = Input.gyro.rotationRateUnbiased.x;
float initialOrientationY = Input.gyro.rotationRateUnbiased.y;
rb.AddForce (initialOrientationY * speed, 0.0f , -initialOrientationX * speed);
}
This code isn't perfect, but it's working, and you get the idea about how it's done.
Answer by Josh Naylor · Oct 10, 2014 at 10:56 AM
Do you mean the Accelerometer? http://unity3d.com/learn/tutorials/modules/beginner/platform-specific/accelerometer-input
Your answer
Follow this Question
Related Questions
Connecting and using a bluetooth gyroscope with an android device 0 Answers
How to setup android build so that users without gyroscope do not see it in Play Store ? 1 Answer
Input.gyro not returning any data on Android 1 Answer
Is it possible to recieve live updates from gyro.attitude on Android? 0 Answers