1
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ 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. ;) \$\endgroup\$ – DMGregory Sep 1 '16 at 12:03
  • \$\begingroup\$ @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). \$\endgroup\$ – MrChips Sep 1 '16 at 12:12
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Sep 1 '16 at 12:18
  • \$\begingroup\$ @DMGregory i thought that was X! Thank for clearing it up for me. \$\endgroup\$ – MrChips Sep 1 '16 at 12:23
3
\$\begingroup\$
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);
    // You can use compass.magneticHeading instead
    // 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);
}
\$\endgroup\$
  • \$\begingroup\$ it didn't work! \$\endgroup\$ – MrChips Sep 1 '16 at 12:30
  • 1
    \$\begingroup\$ "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. \$\endgroup\$ – DMGregory Sep 1 '16 at 12:31
  • \$\begingroup\$ Sorry about that. No errors, and no movement! \$\endgroup\$ – MrChips Sep 1 '16 at 12:33
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Sep 1 '16 at 12:49
  • \$\begingroup\$ 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? \$\endgroup\$ – MrChips Sep 1 '16 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.