Unity Flight Simulation Tutorial

Posted on: March 6th, 2013 by tdc_user 9 Comments

This Unity Flight Simulation tutorial demonstrates how to make a model fly using a simple, customized script. Scroll down for the Flying-Tank3.js script, as well as step-by-step instructions, and controls.

Try the simulation out here (will open a new window).

It needs to be mentioned that the independent developer version of the Unity game engine is free, and can be downloaded at www.unity3d.com.

If you want to see the video tutorials for modeling, xforming, unwrapping, texturing, and exporting the flying vehicle from 3ds Max, go to the 3dcognition Facebook page.

Flying-Tank3.js

var rotateSpeed = 25.0;
var speed = 50.0;

function Update() {
var transAmount = speed * Time.deltaTime;
var rotateAmount = rotateSpeed * Time.deltaTime;

if (Input.GetKey(“up”)) {
transform.Rotate(rotateAmount, 0, 0);

}
if (Input.GetKey(“down”)) {
transform.Rotate(-rotateAmount, 0, 0);
}
if (Input.GetKey(“left”)) {
transform.Rotate(0, -rotateAmount, 0);
}
if (Input.GetKey(“right”)) {
transform.Rotate(0, rotateAmount, 0);
}

if (Input.GetKey (“z”)) {
transform.Rotate(0, 0, rotateAmount);
}

if (Input.GetKey (“x”)) {
transform.Rotate(0, 0, -rotateAmount);
}

if (Input.GetKey (“a”)) {
transform.Translate(0, 0, transAmount);
}

if (Input.GetKey (“q”)) {
transform.Translate(0, 0, (transAmount * 2));
}

}

Process to export the 3ds Max model:
1. Create the model with units set to unitless/default.
2. Make the model within a size that is approximately 200 x 200 x 200.
3. Assume an orientation such that top, front, and right are aligned with the 3ds Max top, front, and right.
4. Once the model is complete, flip it -90 degrees around the x axis.
5. Go to “tools” and apply an xform modifier to the model. This will reset the orientation to be as if you created in this new orientation.
6. Go ahead and apply an unwrap modifier and apply a material if you wish at this point, or just apply a uvw map and a standard material.
7. Select the model, and choose File > Export Selected > and choose filetype of FBX.
8. Turn off all features you don’t need, like animation, etc.

Process to import the 3ds Max model and texture:
1. Make sure the model and texture are in the Assets folder of whatever Project you are working on.
2. Drag the model into your Hierarchy panel.
3. Select the model in the Hierarchy panel and change the Transform rotation to be 0 0 0.
4. Apply a Rigidbody to the model. Make sure the Rigidbody Mass is set to 1, Drag is set to 0.5, Angular Drag is set to 0.05, Gravity is checked, Kinematic is not checked, Interpolate is set to none, Collision Detection is discrete, and Constraints are all unchecked.
5. Apply a Mesh Collider to the model, and make sure that Is Trigger is unchecked, Convex is checked (so it will collide with other objects), Smooth Sphere Collision is unchecked, and Mesh should already be set to the same value as your model.
6. Drag and drop the Flying-Tank3.js script onto the model in the Hierarchy panel.
7. Setup a Main Camera – move a camera so it is behind and above the model, looking slightly down upon the model. Now drag the camera to the model, so it becomes a child of the model.
8. You should be good now to hit the Play button and test out your flight simulator!

Controls:
Left and right arrow keys: Yaw
Up and down arrow keys: Pitch
z and x: Roll
a and q: Slow thrust, Fast thrust

Tags: , , , ,

9 Responses

  1. Mike P. says:

    Very interested in flying games. Just started with Unity 3d, So glad I found you (on youtube). the flying box tut was very helpful…thanks!

    ???anything with a joystick (js)? I hope to stay in touch.

    Thanks again,
    MP

  2. arya.s says:

    what unity version is this ? because i have compiling prob . my version is 4.0.0

  3. abrc says:

    I am a beginner in Unity,but you know….i wanted to do some complex stuff because i felt like it and i ran across your videos in youtube. I really liked your tutorials,keep up the good work! Oh BTW,i also wanted to know if there is any way to make a model spit fire when you press a key. If yes,make a tutorial on that :) (If there is one already,please say it because i still didn’t read all your tutorials.)

  4. tomasla says:

    Great info, thanks for sharing!

Leave a Reply