- Home /
Controll Projectile (rigidbody) with Gyro
Hello everyone.
So basically I have a Ball, and much like a space ship game, the camera is following this ball right behind it while the ball moves forward in the air, and I need to controll it using the Gyro. I simply wanted to do this: (the game is on landscape mode by the way).
If the person tilts the phone left or right (just like a steering wheel), it should move the ball (rigidbody) also left or right respectively.
If the person tilts the phone forward (i.e in such way that the top edge of the phone goes away from the person) then the Ball should go down, and if it tilts backwards (i.e in such way that the top edge of the phone comes closer to the person) then the Ball should go up.
How can I make this? Thank you
Answer by Serge144 · May 09 at 05:29 PM
I was able to do it by doing this code:
// X axis is the Horizontal axis of the phone in landscape, tilting around this axis means to apply force upwards or downwards.
rotX += Input.gyro.rotationRateUnbiased.x * 4f;
// Y axis is the Vertical axis of the phone in landscape, tilting around this axis means to apply force left or right.
rotY += Input.gyro.rotationRateUnbiased.z * 4f;
Rb.AddForce(transform.up * rotX);
Rb.AddForce(-transform.right * rotY);
if(Input.gyro.rotationRateUnbiased.x == 0){
rotX = 0;
}
if(Input.gyro.rotationRateUnbiased.z == 0){
rotY = 0;
}
Your answer
Follow this Question
Related Questions
Dynamically Assign Axes to a float value. 0 Answers
Android gyroscope 3 Answers
Input.gyro.attitude not working on newer Android devices 3 Answers
First person mobile controls easy. 2 Answers
Gyroscope on windows phones. Enabled but no working 0 Answers