# How can i rotate the camera around an object, using compass?

I want the camera to rotate around an object in Y axis, when you rotate your phone. how can i do this?

i already have the code to do it using mouse, but i need it to turn with compass. here's the code for movement with mouse:

    mouseX = Input.GetAxis("Mouse X");
offset = (Quaternion.AngleAxis(mouseX * cameraSpeed, Vector3.up) * offset);
transform.position = point + offset;
transform.LookAt(point);


i know that setting the mouseX value to change with compass direction is my answer, but how can i do it?

• Are you sure you want to rotate on the X axis as you describe? The code you've shown rotates around the world Y axis (which is also the axis of rotation of a compass heading in Unity's system). I think the mouseX may actually be distracting you from a solution here — accumulating horizontal translation from a mouse is not very similar to capturing absolute orientation about the vertical axis from a compass. ;) Commented Sep 1, 2016 at 12:03
• @DMGregory well to be honest, i took help writing this code, and i don't exactly understand it completely. but what i know is that when i swipe my mouse to left and right, the camera rotates around my object in X axis, not Y (camera doesn't go up and down, just goes to left and right). Commented Sep 1, 2016 at 12:12
• Yes, that's a Y axis rotation. The axis of rotation is perpendicular to the movement. (Think of the Earth — it rotates around its north-south axis, and this moves its surface eastward, sweeping the sun across it from east to west). Try taking an empty game object and rotating it in the Y axis in the inspector. You'll see it rotates in the horizontal plane. Commented Sep 1, 2016 at 12:18
• @DMGregory i thought that was X! Thank for clearing it up for me. Commented Sep 1, 2016 at 12:23

void Start() {
// If you need an accurate heading to true north,
// start the location service so Unity can correct for local deviations:
Input.location.Start();

// Start the compass.
Input.compass.enabled = true;
}

void Update() {
Quaternion rotation = Quaternion.Euler(0f, input.compass.trueHeading, 0f);
// if you don't need alignment to true north.

// Position camera at a rotated offset relative to its center point.
transform.position = point + rotation * offset;

// Look at the center point.
transform.LookAt(point);
}

• it didn't work! Commented Sep 1, 2016 at 12:30
• "It didn't work" is never enough information to diagnose a problem. Please describe in detail what went wrong. Did you get an error message? Did you observe no movement? Did it move in an unexpected way (how)? etc. Commented Sep 1, 2016 at 12:31
• Sorry about that. No errors, and no movement! Commented Sep 1, 2016 at 12:33
• Try editing your question to include a larger part of the script, including where you define point and offset. It's possible when integrating this code you left those initialised to zero. Also, verify that the device you're using has a magnetic compass the game has permission to use — try running other apps or samples that use magnetic heading to verify that it's detected correctly. You can also try outputting Input.compass.timestamp to verify new compass readings are being pulled through. Commented Sep 1, 2016 at 12:49
• I printed input.compass.trueheading and Input.compass.timestamp to screen, trueheading was constantly showing 0, and timestamp was constantly showing -1. I tried other apps that use magnetic heading, and they worked perfectly on my phone. Should i still edit my question? Commented Sep 1, 2016 at 13:04