08 October 2016

A HoloLens airplane tracker 3–Creating an annotated airplane

Intro

In this post we are going to create a game object holding the aircraft and a label showing aircraft data that always looks at the user. We are not going to worry about scaling yet – we will handle that from code, in the next blog post.

First some housekeeping

Just as in the CubeBouncer project, we are going to create some folders in the Assets first to keep our custom assets nicely separated. Create a folder App, and in this folder the following subfolders:

  • Audio
  • Materials
  • Prefabs
  • Scripts
  • Textures
  • UWPAssets

Creating the Aircraft Holder and including the aircraft

Inside the HologramCollection, create an empty GameObject called AircraftHolder. Then drag into that object the A320 object form the A320 folder. A rather large A320 appears in the Scene, as indicated in the previous blog post. Just ignore this for now.Open the A320 object (in the hierarchy, not the object in the A320 folder), disable the animator, and make sure X,Y,Z position and rotation are all 0,0,0 and scale 1,1,1:

image

At this point I have no idea what the Animator is supposed to do, but we don’t need it – I think.

Create a label

In this section we create the label above the airplane that displays flight information, like flight number, aircraft type, speed, altitude, etc. Right-click the AicraftHolder and create a “3D Text” Object. Call this “Label”. That label now sits at the bottom of the airplane, where apparently it’s origin is.

Open the label in the inspector, set the Y position to 15 and the Y rotation to 270. That places the text 15 meters above the airplane, placing it parallel above the fuselage, making the default text “Hello World” readable from the right side of the airplane

image

image

The go down to the “Text Mesh”. Change the following things:

  • Anchor to lower center
  • Change font size to 80
  • Change the color to red

image

image

Now hit “Add component”, and add a Mesh Collider. Select the “Convex” checkbox:

image

Of course, it’s not very practical if the airplane’s data is only readable from the left side. So to close off this article, we actually going to write code – by adding a simple script that keeps the text readable from every angle. Go to the Assets/App/Scripts folder, right-click it and hit “Create Script”. Call the script “LookAtCamera”. Double-click it – that will open Visual Studio – and change the update method to this:

void Update()
{	
  gameObject.transform.LookAt(Camera.main.transform);
  gameObject.transform.Rotate(Vector3.up, 180f);
}

imageSave the file, go back at Unity. Drag the new script on top of the Label inside the AircraftHolder game obejct To see this actually works: hit the Unity “Play” button

Now the fun thing is – as long as you are in play mode, changes in settings are not stored so this is great for fooling around. Initially your game screen looks like this – you are looking from under the plane right up to the front landing gear.

image

Select the AircraftHolder in the hierarchy, then change Z to 500.

image

The plane flicks forward… and the text is still readable although we are not looking form the left, but the back. Change rotation X to –20 and rotatation Y to –30:

image

The plane is rotated, we are now looking more or less form the right, but the text is still pointing straight to you, and is readable. Mission accomplished. Not bad for two lines of code!

Exit Play mode. Go to the label once more, and remove the text “Hello World”. The label seems to disappear, but it’s still there, it’s now just empty. It will be filled by code.

And finally…

Select the Prefab folder in Assets/App, and drag the AircraftHolder into it. After you have done it, remove it from the HologramCollection. And save the Scene.

Net result:

image

The reason why I am making the AircraftHolder in stead of adding components directly to the A320 model – which is perfectly possible – is that by using this way I can easily switch out the actual airplane model, while all logic and additional components are preserved. If you find a different (more pretty) aircraft 3D model this way saves you work.

Code so far can be downloaded from Github here.

4 comments:

Amit said...

Got any screen shots or videos on the device?

Joost van Schaik said...

@Amit for sure man! See the first post of the series! http://dotnetbyexample.blogspot.nl/2016/10/a-hololens-airplane-tracker-1.html

Unknown said...

There is a small step that you forgot to mention:
The LookAtCamera script has to be attached to the 3D text "label".

Great blog!

Joost van Schaik said...

Thanks for being sharp @Bram Musters. This has been corrected!