Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Model should face geographical head north.

Discussion in 'Scripting' started by MSSK15, Oct 16, 2020.

  1. MSSK15

    MSSK15

    Joined:
    Mar 30, 2017
    Posts:
    19
    Hi, I am working on a android application in which i will place 3d objects based in geographical location. This is working good. The problem is with rotation. Always the generated gameobject should face geographical North direction. How to do it?
    I have tried

    transform.rotation = Quaternion.Euler(0, -Input.compass.magneticHeading, 0);

    it is not working correctly, showing wrong direction. Is there any other way gameobjects should face geographical north.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    What direction does it actually face? Is there a pattern?

    Assuming that the Y axis is "up" in your world, and that the object is facing towards its local Z axis, and that you are holding your phone flat (because the compass faces out the top), my guess would be that you are doing almost the right thing, but need to add and/or multiply by some constant. Since the appropriate constants depend on a bunch of details of how your game world is set up, the easiest way to figure them out is probably to reverse-engineer exactly what your current attempt is doing.
     
  3. MSSK15

    MSSK15

    Joined:
    Mar 30, 2017
    Posts:
    19
    Firstly Thank you the reply. When instantiated the gameobject is facing the direction of android device. It is behaving same like this in link . Not my application
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Are you creating the object the moment the app starts? You might try monitoring the compass for several seconds after you enable it and see if it "settles down". I don't see any warnings in the documentation about this, but sometimes hardware sensors aren't accurate the instant you turn them on.

    You also might check the value of compass.headingAccuracy to see how accurate Unity thinks it is.

    It might even be helpful to take several readings and average them together. I once made a game that used the compass to orient a map, and I found the map was pretty jittery until I added some time-averaging to the compass readings. (Of course, time-averaging can also make you less accurate while the phone is rotating...)