Saturday, May 18, 2024
HomeGame DevelopmentUnity Navball x,y,z rotation axis' are not tracking main object

Unity Navball x,y,z rotation axis’ are not tracking main object


Disclaimer: I use Godot instead of Unity, so you may have to do some translation.

In Godot it’s possible to choose the order in which rotations are applied – the default is YXZ – this makes sense for first person shooters since the player probably rotates left and right (Y Axis) looks up and down (X axis) and then peeks/tilts the to peek round corners. I am going to stick with this rotation order since once your ship is landed on a planet we can effectively assume a flat world.

I will do a bit of foreshadowing and assume that:

Your ship has fallen over (so the top is pointing North),
hence your nav ball is showing North with the blue side up.

Next we need to define two sets of axes – one for the world and one for nav ball.

The most important information is the bearing (relative to the planet), next we need to know the pitch – if the front of the ship/plane is diving towards the ground, lastly we need to know the roll, given the rotation order above (YXZ) we should lock the Nav Ball to the screen with the axes defined as:

  • Positive X – right of screen.
  • Positive Y – top of screen
  • Positive Z – towards the user.

If we start with the Nav Ball showing a North bearing, because the Y axis has priority, whatever we do with the pitch and roll the bearing will be correct.

The same is true for the pitch (X Axis) since it has priority over the roll (Z Axis)

The main issue with the ship is that “UP” is defined as away from the planet, hence UP will change based on the position of the ship on the planet. Additionally either North or South is defined as a tangent line to the surface of the planet pointing at the pole – East and West are simply normal to both Up and North.

That said we are probably only going to deal with a small part of the planet so we can make some assumptions, within our active area, that:

  • The world is flat.
  • Up is axis aligned to positive Y (Gravity is negative Y)
  • We can define that East is positive X.
  • Hence positive Z is South.

If we start with the ship “fallen over” facing North.
The priority rotation is around the Y Axis, hence whatever we do with pitch and roll the bearing will be correct.

Note: The ship needs to be facing North so that second axis (X) gets applied second, so that that pitch has priority over roll.

I made two assumptions in this post:

  • That the neutral position was with the ship fallen over (facing north).
  • That the rotation order was YXZ.

Using these assumptions works well for: planes, space ships and rovers.
Additionally when landed, most players will probably have the camera slightly above the ground looking down on the scene (you probably won’t need to roll the camera).

All that said if you really don’t like the starting/neutral position being that the ship is flying North you can map the axes differently you will just need to make sure the rotation order is correct for whatever axes mapping you choose.

As outlined above your lander is probably modeled with the top of the lander facing up.

You need to rotate the lander mesh so it’s pointing north.

You can either do this in blender or just add an additional node in your scene tree so that you have two rotations, the higher level one will be the one thats rotated by the game – the lower level node will “fix” your mesh so that it points north.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments