How i can rotate a gameObject with the gyroscope of my phone?,I want to rotate a 2D gameObject using the gyrocope in the phone
Hello, I'm new with this but I want to rotate a gameObject 2D using the gyroscope of my phone, but it's not working, i try to convert the gyroscope data to vertor3 so i can use it in gameObject.transform.LocalRotation, please help me.
bool gyroEnable; Gyroscope gyro;
UnityEngine.Gyroscope guro;
public GameObject fishConteiner;
float speed;
Quaternion rot;
Vector3 rotten;
private void Start()
{
fishConteiner = new GameObject("fishConteiner");
fishConteiner.transform.position = this.transform.position;
transform.SetParent(fishConteiner.transform);
}
private void Update()
{
gyro = Input.gyro;
gyro.enabled = true;
rot = gyro.attitude;
rotten = rot.eulerAngles;
this.transform.rotation *= Quaternion.Euler(rotten);
},Hello, I'm new with this so, i would like to know how i can make the gyroscope rotate my gameObject, I try to convert the quaternion to Vector3 so i can using with gameObject.LocalRotation, but it's not working, please help me.
bool gyroEnable; Gyroscope gyro;
UnityEngine.Gyroscope guro;
public GameObject fishConteiner;
float speed;
Quaternion rot;
Vector3 rotten;
private void Start()
{
fishConteiner = new GameObject("fishConteiner");
fishConteiner.transform.position = this.transform.position;
transform.SetParent(fishConteiner.transform);
}
private void Update()
{
gyro = Input.gyro;
gyro.enabled = true;
rot = gyro.attitude;
rotten = rot.eulerAngles;
this.transform.rotation *= Quaternion.Euler(rotten);
}
Why making things complicated??
Can't you simply
void Update()
{
gyro = Input.gyro;
gyro.enabled = true;
transform.rotation = gyro.attitude;
}
No, because that only work for 3D objects, i want to use the gyroscope in 2D objects (a sprite), so i was trying to convert the quaternion to vector3 so maybe that way i can use that data in localRotation
I can't test right now, but how about
Vector3 eulerAngles = gyro.attitude.eulerAngles;
eulerAngles.x = eulerAngles.y = 0;
transform.eulerAngles = eulerAngles;
I test it, but it doesn't work, maybe i'm forgetting something?, or the answer it's not in this way?, i don't know, but there must be a way, however thank you
I've just tested my code and it works perfectly fine, the sprite rotates on the Z axis using the gyroscope.
Your answer
