Understanding the Gyroscope

Hi Unity,

I’m trying to get the Gyroscope’s current rotation to change the gravity in the game (leaning it left adds to x axis, leaning down adds to y axis…).

I’ve found that attitude is a quaternion, which I don’t know how to relate into vectors that would be useable by Physics.gravity.

the Input.gyro.gravity looks like it is only affected by the current acceleration of the device… So I don’t think that would work with tilting.

Input.gyro.acceleration looks like it is the acceleration as well? That is the example is based on the speed the device is moving around.

// In Update()
		float zGrav, zCurrentGrav;
		
		Input.gyro.enabled = true;
		//transform.rotation = Input.gyro.attitude;
				
		zCurrentGrav = Physics.gravity.z;
		zCurrentGrav += Input.gyro.rotationRate.z * Time.deltaTime;
		Physics.gravity = new Vector3(0, zCurrentGrav, 0)

Can anyone please advice

Well I’ve got it to do something, but the objects with rigid bodies are having an epic panic attack. Does anyone understand the gyroscope and what it’s doing exactly?

	// Called on every frame. Handles touch data and rotates Cube.
    void Update()
	{
        int fingerCount = 0;
		Vector3 CurrentFingerPos; 
		Vector3 newGrav, currentGrav;
		Quaternion gyroRot;
		
		Input.gyro.enabled = true;
		//transform.rotation = Input.gyro.attitude
		
		currentGrav = Physics.gravity;
		
		gyroRot = Input.gyro.attitude;
		
		newGrav = gyroRot.eulerAngles + currentGrav;
		
		Physics.gravity = newGrav;

Like the range that attitude returns?

My phone don’t have gyroscope but Unity3D find gyroscope :slight_smile:

I have no idea why you’re saying that but I agree completely lol :P.

Well after some further study I’ve found some information atleast for anyone else out there reading this.

		// Gyroscope input control
		if(calibrateTimeLapse>0)
		{
			calibrateTimeLapse -= Time.deltaTime;
			
			if(calibrateTimeLapse <= 0)
			{
				Input.gyro.enabled = true;
			}
		}
		
		gyroRot = Input.gyro.attitude;
		
		newGrav = (currentGrav - gyroRot.eulerAngles) * 0.4f;
		
		Physics.gravity = new Vector3(currentGrav.x, newGrav.y, currentGrav.z);
		
		
		MainGame.ScriptLivesTxt.guiText.text = "newGrav: " + newGrav;
		MainGame.ScriptPointsTxt.guiText.text = "currentGrav: " + currentGrav;
		MainGame.ScriptComboTxt.guiText.text = "gyro: " + gyroRot;

Normally the gyroscope is supposed to set the 0.0 of it’s quaternion when it’s enabled, by disabling then re-enabling you can re-calibrate the gyroscope’s default. This is a bug and is not working. So calibration of the gyroscope is done before you’re able to set ‘enable’ to true. So change your results according to testing as gyro’s results are absolute right now

Asides that returning the value of attitude is giving me some interesting results… Here’s what it actually does:

Input.gyro.attitude = [as a quaternion] (X = left/right axis… Right being the positive… Y = Up/Down axis, from default position anti-clockwise looking from the left is the positive…, Z = Upside down… Yeah that’s right. In default position both this and W will be at about -0.9… Only by turning the device upside down exactly do those two floats hit zero).

Hope that helps anyone else out there. Still figuring out how to use that data… but atleast I have some info on their effects now.