Topic: Assign Compass rotation to another GameObject

Hey I see that you have a script that can rotate the camera according to the compass values, but what if I want to rotate another game object that is not the camera according to the accurate compass values? I tried the following based on your code:

    public class RotateCameraByCompassExample : MonoBehaviour
    {

        public GameObject cam2;

        private void Start()
        {
            // Subscribe to compass event
            OnlineMapsLocationService.instance.OnCompassChanged += OnCompassChanged;
        }

        /// <summary>
        /// This method is called when the compass value is changed.
        /// </summary>
        /// <param name="f">New compass value (0-1)</param>
        private void OnCompassChanged(float f)
        {
            // Rotate the camera.
            OnlineMapsTileSetControl.instance.cameraRotation.y = f * 360;

            Vector3 rot = new Vector3 (0f, f * 360f, 0f);

            cam2.transform.eulerAngles = rot;

        }
    }

The problem is that the rotation of my gameobject is veryyyy jerky. It's unusable, am I doing it wrong?

Re: Assign Compass rotation to another GameObject

Hello.

The problem is not in your code, and not in Online Maps.
The problem is in the sensor values.
Use Mathf.Lerp to smooth the values of the compass sensor.

Something like:

using System;
using UnityEngine;

public class LerpCompassValue:MonoBehaviour
{
    public GameObject cam2;
    private float compassValue;
    private float currentCompassValue;
    private bool isFirstValue = true;

    private void Start()
    {
        OnlineMapsLocationService.instance.OnCompassChanged += OnCompassChanged;
    }

    private void OnCompassChanged(float f)
    {
        currentCompassValue = f;
        if (isFirstValue)
        {
            compassValue = f;
            isFirstValue = false;
        }

        if (currentCompassValue - compassValue > 0.5) compassValue += 1;
        else if (currentCompassValue - compassValue < -0.5) compassValue -= 1;
    }

    private void Update()
    {
        if (isFirstValue) return;
        if (Math.Abs(currentCompassValue - compassValue) < float.Epsilon) return;
        if (Mathf.Abs(currentCompassValue - compassValue) < 0.001f) compassValue = currentCompassValue;
        else compassValue = Mathf.Lerp(compassValue, currentCompassValue, 0.05f);

        OnlineMapsTileSetControl.instance.cameraRotation.y = compassValue * 360;
        Vector3 rot = new Vector3(0f, compassValue * 360f, 0f);

        cam2.transform.eulerAngles = rot;
    }
}
Kind Regards,
Infinity Code Team.

Do not know the best way to thank the developer? Rate the asset in Asset Store!

Re: Assign Compass rotation to another GameObject

Im still seeing some issues, is it because in my app the phone needs to be used upright in portrait mode, meaning the top of the screen is facing up?

Re: Assign Compass rotation to another GameObject

In that case, I do not understand the problem.
Please explain in detail what you mean "rotation of my gameobject is veryyyy jerky", and why the orientation of the phone is important for this problem.
Perhaps a video or screenshot will help to understand the problem faster.

Kind Regards,
Infinity Code Team.

Do not know the best way to thank the developer? Rate the asset in Asset Store!

Re: Assign Compass rotation to another GameObject

No problem I attached a video showing the problem with the game camera as the game object to be moved and a video background to show the orientation of the phone properly. See at the beginning the back of the phone is facing down, and I pointed the phone North. The z axis of the camera is rotating according to the compass so at north the map seems flat. When we I move in a circle it moves accordingly. Now when you see me move the phone up to where the screen is facing my face everything becomes troublesome.

6 (edited by renegade.bullet678 2017-09-12 17:27:43)

Re: Assign Compass rotation to another GameObject

No problem I attached a video showing the problem with the game camera as the game object to be moved and a video background to show the orientation of the phone properly. See at the beginning the back of the phone is facing down, and I pointed the phone North. The z axis of the camera is rotating according to the compass so at north the map seems flat. When we I move in a circle it moves accordingly. Now when you see me move the phone up to where the screen is facing my face everything becomes troublesome.

Here is the video, it can't be attached because it's too large (10 mb)

https://www.dropbox.com/s/0gfir9y4q7s3n … 1.m4v?dl=0

Re: Assign Compass rotation to another GameObject

The compass is used when the phone is placed horizontally (screen up).
If you want to use the phone vertically (screen to user) you need to use a gyroscope.
https://docs.unity3d.com/ScriptReference/Gyroscope.html

Kind Regards,
Infinity Code Team.

Do not know the best way to thank the developer? Rate the asset in Asset Store!

Re: Assign Compass rotation to another GameObject

How do I get true north with only using the gyroscope? What if the user is facing south when the app first starts, the orientation is 0,0,0, but if I have the game object facing north then 0,0,0 will be north while the user is really facing south? Google maps has done it accurately.

Re: Assign Compass rotation to another GameObject

If you want the north in the application to point to the north in the real world, you need to use a compass.
But you will get the wrong compass values in the phone's vertical orientation.

Online Maps Location Service is just a wrapper for Unity Location Service and Unity Compass.
Online Maps Location Service takes this value from Unity Compass trueHeading.

Yes, in Google Maps it works much better.
But using Online Maps, if you do not like how some feature works (in your case, the compass), you can implement it yourself without modifying the code, and get the behavior that you need.

I tried to make a script that combined the data from the gyro and the compass, but so far without success.
I think this is the right direction to work around the problem. But I can be wrong.

Kind Regards,
Infinity Code Team.

Do not know the best way to thank the developer? Rate the asset in Asset Store!

Re: Assign Compass rotation to another GameObject

Thank you for answering, I fixed the problem. It was quite a hurdle because Im shaky with my trigonometry.

Re: Assign Compass rotation to another GameObject

renegade.bullet678 wrote:

Thank you for answering, I fixed the problem. It was quite a hurdle because Im shaky with my trigonometry.

Would it be possible to see your solution?  I've been having the same problem as you and was pulling my hair out.   Could you help?  PM sent.  Thanks.