- Home /
Gyroscope steering - set default position of phone
I have a question about gyroscope delault position. I created a game in witch you move space ship foward, back, left, and right by moving the phone(using gyroscope). My problem is that I must hold phone like on picture 1. If i hold the phone like on picture 2, the ship goes down. Anyone know how to change default position of the gyroscope, so I can normally hold the phone and steering the ship? I want the ship stationary if my phone is holded at 45 degrees. Otherwise, it move to the titled direction.
Picture 1 shows how i hold the phone to have the space ship in the center, and picture 2 shows how i want to hold the phone have the space ship in the center.
Anybody know how to do that?
My code for steering the ship:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundaryy
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerControll1 : MonoBehaviour {
public float speed;
public float tilt;
public Boundaryy boundary;
void Start () {
Input.gyro.enabled = true;
Screen.sleepTimeout = SleepTimeout.NeverSleep;
}
void FixedUpdate() {
float xi = Input.gyro.userAcceleration.x;
float yi = Input.gyro.userAcceleration.y;
Vector3 movement = new Vector3 (xi, 0.0f, yi);
movement = Quaternion.AngleAxis(45, Vector3.right) * movement;
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
Answer by campeanionut93 · Jan 15, 2019 at 03:53 PM
I know this is an old question, but maybe somebody else stumbles upon this.
You might want to look into Input.gyro.gravity. That returns a Vector3 with the real world gravity that is applied to each axis, in the range [0, 1] (where 0 is no gravity i.e. the axis is perpendicular on the gravity vector, and 1 is full gravity i.e. the axis is aligned with the earth's gravity vector).
For example, if you hold your phone like in picture 1, the gravity on the Y axis (top to bottom of your phone, depending on orientation) would be 0. In picture 2, assuming you hold your phone at a 45 degree angle, the gravity value for Y would be sine(45 deg) ~= 0.7.
If we apply some simple trigonometry (search google for the trigonometric circle and sine function), we basically want to offset the graph of the sine function. Since we know that Input.gyro.gravity.y is the sine of the y axis angle relative to the gravity vector, we can do this:
float NormalizeGravity(float axisValue, float defaultRotationInDegrees)
{
return Mathf.Sin(Mathf.Asin(axisValue) + Mathf.Deg2Rad * defaultRotationInDegrees);
}
DISCLAIMER: I tested this in landscape mode. You may also need to take care of the sign of the Sin and Asin functions.
Your answer
Follow this Question
Related Questions
Steering Wheel with mouse drag 0 Answers
Get Gyro 3-Axis Angle of Smartphone in Unity 0 Answers
2d Raycasting, trouble drawing rays at the correct angles 1 Answer
Steering Overcompesation 0 Answers