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?
// Update is called once per frame
void Update()
{
transform.localRotation = Quaternion.Euler(0, 360 - transform.root.rotation.eulerAngles.x, 0);
}
}
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.
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.
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.
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.
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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CompassCardMapEulerAngles : MonoBehaviour
{
public Transform CompassPivot;
void Update ()
{
float OurHeading = transform.rotation.eulerAngles.y;
CompassPivot.localRotation = Quaternion.Euler( 0, 0, OurHeading);
}
}