Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Magnetic compass

Discussion in 'Scripting' started by cruising, Nov 30, 2019.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    308
    Hello!

    Im trying to get my airplane compass to work correctly. But what ever i do it only rotates in Y and Z axis instead of X.
    Its a gauge that faces you and should rotate like a clock to North, but it does not. i have tried to change each 0 to 360 and also changed X Y Z with o success.
    Any tips?

    Code (csharp):
    1.  
    2.     // Update is called once per frame
    3.     void Update()
    4.     {
    5.         transform.localRotation = Quaternion.Euler(0, 360 - transform.root.rotation.eulerAngles.x, 0);
    6.     }
    7. }
    8.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    5,499
    Do you mean compass for heading, or like an artificial horizon attitude indicator??

    A compass usually only goes left/right, i.e., rotates around the Y axis.

    To get assets rotating correctly around a particular axis, it can be helpful to insert extra levels of parent/child GameObjects above them, and manipulate each one independently.

    I think in the specific case you outline above, you would take YOUR eulerAngles.y and slave it over to the compass sub-face objects "Z" rotation.

    An attitude indicator is a little trickier, but the same ideas apply.
     
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    308
    It is a compass like this one




    I already have one like this working, but i also want the other one to work. But i cant figure out how.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    7,247
    It is exactly same thing. Instead rotating a circle, with printing on the top, you rotate instead a cylinder, with printing on the side.
     
  5. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    308
    Yes i know, but the problem is, if i take a cylinder and fit it to the compass gauge it flips the cylinder flat on Y axis and rotate flat, not like a clock on the wall as it should be.

    And if i edit the script so the cylinder stands like a clock, then the cylinder rotates like a spinning coin on the table.
     
    Last edited: Nov 30, 2019
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    7,247
    upload_2019-11-30_16-44-48.png

    You need rotate along local Y axis.
    Y axis is default axis.

    Not sure what that means.
    Most likely, you are doing something wrong
     
  7. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    308
    Ill try explain a bit better.
    Take that cylinder on your picture, rotate it so the flat side is facing your eyes. That flat side shows the compass image, shrink the length so it looks more like a clock (like one on the wall that shows you what time it is).
    Now you have the compass as it should look and be, then add that scrip i posted and press play, then the green axis Y will flip and point to the sky and show you the rounded side instead of the flat side.

    That is what my problem is with the code, Y axis get flipped 90 degrees on start.
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    7,247
    After startup, is Y axis pointing in same direction what Y axis of compass body (parent GameObject)?
    You flipping issue is, as your body Y axis is 90 degree offset, from cylinder Y axis.

    Can you confirm that?

    If so, make sure, that compass body points Y axis in same direction as cylinder (default should be up for both).
    Possibly you may need flip 90 degree body mesh, to look correct.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    5,499
    This is what I wrote about above, the second-to-last paragraph in my first post.

    Press PLAY and then rotate the GameObject called PlayerAircraft around the Y axis.

    I have no idea what happens if you're doing loop-de-loops... might need some extra math in the script to invert rotation when your transform.up points downwards...

    Full package below, and here is the main script, which is trivial:

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CompassCardMapEulerAngles : MonoBehaviour
    6. {
    7.     public    Transform    CompassPivot;
    8.  
    9.     void Update ()
    10.     {
    11.         float OurHeading = transform.rotation.eulerAngles.y;
    12.  
    13.         CompassPivot.localRotation = Quaternion.Euler( 0, 0, OurHeading);
    14.     }
    15. }
    It does not run in editor time.
     

    Attached Files:

    Antypodish likes this.
unityunity