Rotate a Quaternion
I'm hitting a brick wall right now and it's really discouraging (is there any other kind?). Anyway here's my issue. I've made a very simple script that ties my gyro to my camera. It rotates as expected with one flaw, it starts at an unintuitive offset.
I consider my phones camera to be the direction I'm looking in. However when the game starts Phone Camera down corresponds to game camera forward.
I'm going to paste my code in along with a comment showing how I tried to rotate the quaternion. Here's some things I've tried:
Multiply by (0,1,0,0) and all kinds of variations using the sin(x/2) with no luck. It seems like it only flips 180 degrees or nothing at all. I need a rotation of 90 around the y axis (which I realize is slot 3 or I think I realize that)
I tried to rotate it with euler angles which worked for some rotations but yielded unexpected motion in some areas
I tried rotating the quaternion around an axis, I was able to get it to face the correct direction by rotating it 90 about vector.left but then roll became yaw and visa versa and I couldn't fix it again.
I've also tried setting other vectors as forward with no luck.
Can someone please tell me what I'm missing? The only alternative I can think of is to rotate the game world by 90 degrees and I'd honestly rather just turn off the comp and go drink rather then mess with that right now.
PLEASE TAKE THE CODE BELOW WITH A GRAIN OF SALT. I'M USING IT AS A TEST SCRIPT AND REALIZE IT HAS SOME UNNECESSARY PORTIONS.
using UnityEngine;
using System.Collections;
public class HeadController : MonoBehaviour
{
public bool IsGyroAvailable;
void Start ()
{
Screen.sleepTimeout = SleepTimeout.NeverSleep;
//Need to spruce this up a bit, if gyros aren't present we need to update with a combo of accelerometers and compass inputs
if (1== 1)
{
Input.gyro.enabled = true;
IsGyroAvailable = true;
}
else
IsGyroAvailable = false;
}
void Update ()
{
if (IsGyroAvailable)
{
this.transform.localRotation = Input.gyro.attitude;
//this.transform.rotation *= Quaternion.AngleAxis(90, Vector3.up);
// this.transform.rotation *=Quaternion.Euler(90f, 0, 0);
//this.transform.rotation*= *=new Quaternion(0, 1, 0, 0);
}
else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 TouchDelta = Input.GetTouch(0).deltaPosition;
transform.Rotate(TouchDelta.y / 2, -TouchDelta.x / 2, 0, Space.Self);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612174054im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get single axis from gyroscopic input (iOS) 0 Answers
How can I work out the orientation of my rotatable cube via script? 1 Answer
Ignore gyroscope yaw 0 Answers
VR without a headset!? 2 Answers