Overview
In this post we’re going to show you how to create a real world Flight Simulator using the WRLD SDK. The WRLD SDK provides streamed 3D models of the real world so we’re going to spend most of this post teaching you how to create an aircraft with a simple flight model and a controller for the camera to follow the aircraft.
The amount of coding required to do this is pretty light - less than 25 lines once you ignore the Unity boilerplate and the explanatory comments. Here’s a video showing what you could build:
While the flight and camera models we give here are pretty basic they should give you a solid basis for building something more sophisticated. At the end of the post we’ll give you suggestions for directions you could take this without too much effort. If there’s sufficient interest we’ll dig into some of these in future posts!
Before you start
To follow along with this tutorial you’re going to need the following:
-
Unity 2018.1.0f2 or later, running on Windows or macOS (available here)
-
WRLD Unity SDK 0.7 or later (available here)
-
A WRLD account (sign up for a trial here)
-
wrld-flight-simulator-tutorial.zip file containing the assets and code for this tutorial (available here)
Create a Unity project and import the WRLD SDK
First we need to create a fresh Unity project for Flight Simulator and import the WRLD Unity SDK into it as an asset.
Once you’ve done this, your project will have access to streamed 3D models of the real world.
-
Open Unity and create a new Project using the 3D template, give it a name (I called mine “Flight Simulator”) and click Create Project.
-
Add the WRLD SDK to your new project. This is covered in our Getting Started Instructions here. These instructions guide you through the process of adding the WRLD SDK Asset to your Project, signing up for a WRLD account and creating an API Key for your Project.
When you add the Unity SDK to your project you will be asked whether you want to increase the Shadow Distance settings, you should answer Increase to this question. The reason for this is that real world environments are typically larger than a traditional game level and consequently we want to see shadows over a larger distance.
First Run - Welcome to San Francisco
By the time you reach the end of the Getting Started instructions, you should have the UnityWorldSpace scene open in Unity. When you press Unity’s Play button you should see the model for downtown San Francisco stream into view.
The WRLD SDK comes with simple camera controls out of the box so you can click drag to move around and explore the environment.
Once you’re done exploring press the Play button again to stop the project running. Throughout the rest of this post you’ll need to stop the project running before you continue with the tutorial. If you don’t do this, any changes you make will be temporary and will be reverted when you stop the project. So get it into the habit of stopping the project before moving on.
Now use File → Save Scene as…, and save the Scene as “FlightSimulatorScene” .
Adding an Aircraft
Now that we have the world in our Project, we need to create an aircraft to fly around it. We’ll use a Unity GameObject to represent the Aircraft.
Importing the aircraft mesh
In the zip file for this tutorial we have provided a textured aircraft mesh that you can use. Here’s how to import it into your Project:
-
Unzip the wrld-flight-simulator-tutorial.zip file somewhere on your hard drive.
-
Choose Assets → Import New Asset… from the Unity menu.
-
Navigate to the folder where you unzipped wrld-flight-simulator-tutorial.zip, go to the models directory, choose the SpaceJet.DAE file and click Import.
-
You should now see a new asset in your Project’s Assets folder called SpaceJet.
Adding the aircraft to your Scene
Now that you have the aircraft mesh in your Project you need to add it the Flight Simulator Scene.
-
Open the SpaceJet asset by clicking the small arrow button to the right of it. This shows a selection of Assets that Unity created for us when importing the model.
-
We just want the mesh asset which is the one on the far right, click-drag it into your scene just below the WrldMap Object.
-
Click on the SpaceJet in the Scene view to select it.
-
Rename it from SpaceJet to Aircraft.
-
Now in the Inspector window on the right we need to set the aircraft’s initial position and scale:
-
Set the Position to 0, 500, -500
-
Set Scale to 5, 5, 5
-
-
Now press Play again and you should see a bright pink aircraft sitting over San Francisco.
The WRLD SDK uses metres as its unit of measurement and in this scene the positive Y axis points upwards from the ground, so our aircraft is 500 metres above the ground.
Make the aircraft look more realistic with a texture and a material
Let’s add a texture to the aircraft to make it look a bit more realistic. This is a three step process:
-
Import the texture.
-
Create a new Material that uses the texture.
-
Apply the Material to our aircraft object.
Import the texture
-
Import Choose Assets → Import New Asset… from the Unity menu.
-
Navigate to the folder where you unzipped wrld-flight-simulator-tutorial.zip, go to the models/texture directory, choose the SpaceJet_AlbedoTransparency_Blue.png file and click Import to import the texture.
-
You should now see the texture appear in your Project’s Assets folder.
Create a Material to apply the texture
-
Next we need to create a Material to apply the texture to our aircraft.
-
Select Assets → Create → Material from the Unity menu. Give the new material a descriptive name like “SpaceJetMaterial”
-
Now click on the new Material to select it, then in the Inspector window click the small “target” icon next to the Albedo field to choose the texture to use. Choose the SpaceJet_AlbedoTransparency_Blue texture that we imported in step 2 above.
Apply the Material to the aircraft
-
Click on the Aircraft object in the scene view to select it.
-
In the Inspector window on the right, go to Mesh Renderer → Materials and click the target icon next to Element 0.
-
Now choose the SpaceJetMaterial we created above.
-
Press Play and you should see the aircraft rendered more realistically with a blue and grey texture.
Making our aircraft move with a physics based flight model
Now that we have our aircraft looking pretty realistic and positioned in the world we need to get it moving. We’re going to do this by attaching a flight model to the Aircraft GameObject. This flight model will take inputs from the player and use them to move the Aircraft around.
Now flight models are a deep subject and they can get pretty complicated. It’s certainly possible to build a sophisticated flight model that accurately simulates a real world aircraft. You might find a flight model like this in a dedicated training simulator used to train airline pilots.
But, in our case we’re not striving for physically accurate modelling of the pilot experience. We want something that looks pretty realistic, is quick to code and is easy for players to pick up and play. This means that our flight model can be much simpler.
Applying physics to our aircraft
We’re going to use Unity’s rigid body physics simulation as the basis for our flight model. In this model our aircraft will be represented by a physical entity with a limited set of properties like “mass”. Then we’ll move that physical entity around by applying forces to it based on the player’s input.
The physical properties that we are going to add to our aircraft are represented by Unity’s RigidBody component so in order to add physics to our aircraft, all we need to do is attach a RigidBody component to it like this.
-
Click on aircraft in the Scene view to select it.
-
In the Inspector view, click Add Component at the bottom of the aircraft’s components.
-
Choose Physics → Rigidbody from the menu.
Now when you click Play you will see our aircraft falling towards the ground. This is because the force of gravity is pulling it downwards and we haven’t yet added opposing forces to counteract gravity and keep it in the air.
Since our objective was to create a flying aircraft, rather than a falling stone, let’s push on to add some more forces so we can fly our aircraft around.
Moving the aircraft with a flight model
Next we need to get our Aircraft moving forwards and give the player some control over its direction.
We’re going to do this by attaching a short Script to the aircraft GameObject. This script will read input from the keyboard, calculate a force to apply to the RigidBody and then apply that force to move the aircraft around the world. We’ll call this script “AircraftController”.
Attaching the AircraftController script
-
Import Choose Assets → Import New Asset… from the Unity menu.
-
Navigate to the folder where you unzipped wrld-flight-simulator-tutorial.zip, choose the AircraftController.cs file and click Import to import it into your project.
-
You should now see the script appear in your Project’s Assets folder.
-
Now drag the AircraftController script from your Project’s Assets folder onto the Aircraft object in the Scene view.
-
Once you’ve done this, select the Aircraft in the Scene view and should see the AircraftController script at the bottom of the Inspector view, with a few properties like “Max Speed” which define how the aircraft should behave.
Now click Play and you should see the aircraft flying/falling away from you. But now you can take control with the either the cursor keys or the WASD keys to steer the aircraft away from disaster. Pressing the up or W key will cause the aircraft to pull up before it falls to the ground.
You’ll see the aircraft soon flies off screen and becomes impossible to control. It’s still moving around somewhere out there, we just can’t see it because it is out of view of the camera. To fix this we’ll add another script to the camera so that it follows our aircraft as it flies around the world.
Adding a “follow camera” to keep the aircraft in view
Like flight models, camera controls can get extremely complicated if we let them. But to get us started we’re going to use a simple model based on a bias function. Bias functions have a long history in game programming because they’re simple to code, cheap to compute and give a nice smooth feel to the camera movement.
Our bias function camera controller works like this:
-
Compute an ideal position for the camera. In our case this will be behind and slightly above the aircraft.
-
Compute the difference between the camera’s current position and the ideal position.
-
Move the camera a fraction of the difference, so that it gets closer to the ideal position.
By executing these steps once every frame, the camera will gradually, but not instantly, move towards its ideal position. When the camera is a long way from its ideal position it will move faster, then as it gets closer to the ideal position it will move more slowly. This gives the camera a nice smooth movement that doesn’t distract the player from the objective of the game (controlling the aircraft in this case).
The process for adding the camera controller script is similar to the one we used for the aircraft controller, except in this case we need to attach the script to the Main Camera object instead of the aircraft.
Adding the camera controller script
-
Choose Assets → Import New Asset… from the Unity menu.
-
Navigate to the folder where you unzipped wrld-flight-simulator-tutorial.zip, choose the CameraController.cs file and click Import to import it into your project.
-
You should now see the script appear in your Project’s Assets folder.
-
Now drag the CameraController script from your Project’s Assets folder onto the Main Camera object in the Scene view.
-
Once you’ve done this, select the Main Camera in the Scene view and you should see the CameraController script at the bottom of the Inspector view, with a few properties like “Follow Bias” which define how the camera should move.
-
Now we need to tell the CameraController to follow our aircraft object. To do this drag the Aircraft object from the Scene view onto the CameraController’s Object To Follow property in the Inspector view.
If you forget to do this, you will see an UnassignedReferenceException when you run the project. This is Unity’s way of telling you that a script is expecting a reference to an object, but you not have provided one.
Now hit Play again and you’ll see the camera start to follow the aircraft as you pilot it about. But something’s not right - the camera isn’t keeping up so the aircraft heads off into distance, leaving the camera behind.
Disabling the WRLD built-in camera controls
To fix this we need to think back to the beginning of this tutorial. Do you remember we talked about how the WRLD SDK provided built-in camera controls that allow you to move around the world? We haven’t done anything to disable these controls, so they’re going to be fighting with our new CameraController for control of the Main Camera object.
So the camera behaviour we’re seeing is some weird combination of the WRLD built-in camera controls and our new aircraft following camera controls. To fix this we need to disable WRLD’s built-in camera controls so that the CameraController can run the show on its own.
-
Click on the WrldMap object in the Scene view to select it.
-
In the Inspector view, untick the “Use Built-in Camera Controls” checkbox to disable WRLD’s camera controls.
Now press Play again and you should see the camera behaving more sensibly, sitting more or less behind the aircraft as you pilot it around the world.
Where to next?
That concludes this tutorial, but there’s a whole host of things you can do from here. Here’s a few suggestions for things you might like play around with.
Different Cities
Try different locations around the WRLD by setting the Start Latitude and Start Longitude properties on the WrldMap object. Here are a few you might like to try:
-
London: 51.497980, -0.122835
-
New York: 40.697050, -74.009341
Collision Detection
You’ll notice that the aircraft doesn’t react to collisions with the ground or buildings. Try enabling collision detection and reacting to collisions for a more realistic and challenging game. The Collision Settings section of the WrldMap object is a good place to start. You can find example code here in the WRLD SDK docs here to guide you.
Pickups
Add floating objects for the pilot to pick up as rewards. Use GameObjects for the pickups and WRLD’s GeographicTransform component to position them at real world latitude / longitude positions.
Different aircraft types
Create different aircraft behaviours by playing around with the properties of the AircraftController object. Can you make the controllers super-responsive like a stunt plane? Or slow and lumbering like a cargo carrier?
More realistic flight model
Make the flight model more realistic and interesting. You’ll notice that aircraft doesn’t bank to the side when turning left or right so maybe you could add that to the flight model.
If there’s sufficient interest in any of the above topics we’ll do a follow up post to dig into them more deeply. So let us know what you’re working on with and what you’d like us to cover next. #WRLDFlightSim
And as always, you get can support by emailing support@wrld3d.com. So just shout if you need any help and advice to get your project under way.