# Unity testproject - a little flightsim

Discussion in 'Made With Unity' started by Tiles, Mar 19, 2010.

1. ### Rush-Rage-Games

Joined:
Sep 9, 2010
Posts:
1,998
Code (csharp):
1. // -------------------- Kalkulationen Salto vorwärts abblocken / Calculations Block salto forward -----------------------------------------------------
2.
3.         // Variable divesalto
4.         //  sturzflug salto vorwärts blocken || dive salto forward blocking
5.         if (rotationx < 90) divesalto=rotationx/100.0;//Updown
6.         if (rotationx > 90) divesalto=-0.2;//Updown
7.
8.         //Variable diveblocker
9.         // Blockt seitlichenTaumelflug beim Sturzflug || blocks sideways stagger flight while dive
10.         if (rotationx <90) diveblocker=rotationx/200.0;
11.         else diveblocker=0;
12.
13.         //----------------------------Alles zurückdrehen / everything rotate back ---------------------------------------------------------------------------------
14.
15.         // Zurückrotieren wenn Key in die andere Richtung zeigt | rotateback when key wrong direction
16.         if ((rotationz <180)(Input.GetAxis ("Horizontal")>0)) transform.Rotate(0,0,rightleftsoft*Time.deltaTime*80);
17.         if ((rotationz >180)(Input.GetAxis ("Horizontal")<0)) transform.Rotate(0,0,rightleftsoft*Time.deltaTime*80);
18.
19.         //Zurückdrehen Z Achse generell. Limitiert auf Horizontal Button ist nicht gedrückt
20.         //Rotate back in z axis general, limited by no horizontal button pressed
21.         if (!Input.GetButton ("Horizontal")){
22.             if ((rotationz < 135)) transform.Rotate(0,0,rightleftsoftabs*Time.deltaTime*-100);
23.             if ((rotationz > 225)) transform.Rotate(0,0,rightleftsoftabs*Time.deltaTime*100);
24.             }
25.
26.         //Zurückdrehen X Achse || Rotate back X axis
27.         if ((!Input.GetButton ("Vertical"))(groundtrigger.triggered==0)){
28.             if ((rotationx >0)(rotationx < 180)) transform.Rotate(Time.deltaTime*-50,0,0);
29.             if ((rotationx >0)(rotationx > 180)) transform.Rotate(Time.deltaTime*50,0,0);
30.             }
31.
32.     //----------------------------Geschwindigkeit Fahren und Fliegen / Speed driving and flying ----------------------------------------------------------------
33.
34.         // Geschwindigkeit || Speed
35.         transform.Translate(0,0,speed/20*Time.deltaTime);
36.
37.         //Wir brauchen ein minimales Geschwindigkeitslimit in der Luft. Wir limitieren wieder mit der groundtrigger.triggered Variable
38.         //We need a minimum speed limit in the air. We limit again with the groundtrigger.triggered variable
39.
40.         // Input Gas geben und abbremsen am Boden|| Input Accellerate and deccellerate at ground
41.         if ((groundtrigger.triggered==1)(Input.GetButton("Fire1"))(speed<800)) speed+=Time.deltaTime*240;
42.         if ((groundtrigger.triggered==1)(Input.GetButton("Fire2"))(speed>0)) speed-=Time.deltaTime*240;
43.
44.                 // Input Gas geben und abbremsen in der Luft|| Input Accellerate and deccellerate in the air
45.         if ((groundtrigger.triggered==0)(Input.GetButton("Fire1"))(speed<800)) speed+=Time.deltaTime*240;
46.         if ((groundtrigger.triggered==0)(Input.GetButton("Fire2"))(speed>600)) speed-=Time.deltaTime*240;
47.
48.         if (speed<0) speed=0; //floatingpoint calculations makes a fix necessary so that speed cannot be below zero
49.                                             //Floatingpoint Kalkulation macht ein Fix nötig damit Speed nicht unter Null sein kann
50.
51.         //Another speed floatingpoint fix:
52.         if ((groundtrigger.triggered==0)(!Input.GetButton("Fire1"))(!Input.GetButton("Fire2"))(speed>695)(speed<705)) speed=700;
53.
54.         //-----------------------------------------------------Auftrieb/Uplift  ----------------------------------------------------------------------
55.
56.         //Wenn in der Luft weder Gasgeben noch Abbremsen gedrückt wird soll unser Flugzeug auf eine neutrale Geschwindigkeit gehen.
57.         //Mit dieser Geschwindigkeit soll es auch neutral in der Höhe bleiben. Drüber soll es steigen, drunter soll es sinken.
58.         //Auf diesem Wege lässt sich dann abheben und landen
59.         //When we don`t accellerate or deccellerate we want to go to a neutral speed in the air. With this speed it has to stay at a neutral height.
60.         //Above this value the airplane has to climb, with a lower speed it has to  sink. That way we are able to takeoff and land then.
61.
62.         //Neutrale Geschwindigkeit bei 700 || Neutral speed at 700
63.         //Dieser Code stellt in der Luft die Geschwindigkeit auf 700 zurück wenn nicht gasgegeben oder abgebremst wird. Maximum 800, minimum 600
64.         //This code resets the speed to 700 when there is no acceleration or deccelleration. Maximum 800, minimum 600
65.         if((Input.GetButton("Fire1")==false)(Input.GetButton("Fire2")==false)(speed>595)(speed<700)) speed+=Time.deltaTime*240.0;
66.         if((Input.GetButton("Fire1")==false)(Input.GetButton("Fire2")==false)(speed>595)(speed>700)) speed-=Time.deltaTime*240.0;
67.
68.         //uplift - Auftrieb
69.         transform.Translate(0,uplift*Time.deltaTime/10.0,0);
70.
71.         //Uplift kalkulieren. Der Auftrieb || Calculate uplift
72.         uplift = -700+speed;
73.
74.     //Wir wollen am Boden keinen Abtrieb. Wenn die Uplift am Boden kleiner 0 ist, setzen wir sie auf 0.
75.     //We don`t want downlift. So when the uplift value lower zero we set it to 0
76.         if ((groundtrigger.triggered==1)(uplift < 0)) uplift=0;
77.
78.     // ------------------------------- Rumfahren / driving around  ------------------------------------------------------------
79.
80.     //Special case landschaft überfahren. Wir benötigen sowas wie Pseudo Gravitation.
81.     //Und wir richten das Flugzeug am Untergrund aus
82.     //Verwendet werden Sensorobjekte.
83.
84.     //special case drive across landscape. We need something like pseudo gravitation.
85.     //And we align the airplane at the ground.
86.     //We use sensorobjects for that
87.
88.     //Fahren auf Grund geht bis Speed 600. Fünf Punkte Sicherheit || ground driving is up to Speed 600. Five points security
89.     if (speed <595){
90.     //Nase runter beim Hügelrunterfahren
91.     if ((sensorfront.sensorfront ==0)(sensorrear.sensorrear ==1)) transform.Rotate(Time.deltaTime*20,0,0);
92.     //Nase hoch beim Hügelrunterfahren
93.     if ((sensorfront.sensorfront ==1)(sensorrear.sensorrear ==0)) transform.Rotate(Time.deltaTime*-20,0,0);
94.     //Nase hoch beim Hügelanfahren
95.     if (sensorfrontup.sensorfrontup ==1) transform.Rotate(Time.deltaTime*-20,0,0);
96.     //Pseudoschwerkraft. Könnte man naürlich noch verfeinern indem man den Betrag erhöht.
97.     if (groundtrigger.triggered==0) transform.Translate(0,pseudogravitation*Time.deltaTime/10.0,0);
98.     }
99.
100.     //--------------------------------------------- Debug ---------------------------------------------------------------------------------
101.     //Mit Key 1 über den Buchstaben kann man sich in die Höhe von 200 katapultieren. Mit Geschwindigkeit 700. Zum debuggen.
102.     //Damit man nicht jedesmal starten muss ...
103.     //With key 1 above the letters you can set the airplane to height 200. With speed 700. For debug reasons.
104.     // so that you don`t have to takeoff all the time ...
105.
106.     if (Input.GetKey ("1")) {
107.     transform.position.y=200;
108.     speed=700;
109.         }
110.
111.     }
112.     //-------------------------------------------------- Limiting to playfield --------------------------------------------------------------------------
113.
114.     //Here i wrap the airplane around the playfield so that you cannot fly under the landscape
115.     //Hier wird das Flugzeug zur anderen Seite des Levels transportiert wenn es dem Rand zu nahe kommt. Damit ihr nicht unter die Landschaft fliegen könnt
116.     if (transform.position.x >= 900.0) transform.position.x = 0;
117.     else if (transform.position.x <= -900.0) transform.position.x = 900.0;
118.     else if (transform.position.z >= 900.0) transform.position.z = 0;
119.     else if (transform.position.z <= -900.0) transform.position.z = 900.0;
120.
121.     //Hier wird die Höhe limitiert || Here i limit the height
122.     if (positiony > 1000) transform.position.y = 1000;
123.
124. }
125.
126. // ----------------------------------------------  Gameover activating ----------------------------------------------------------------
127.
128. //Wenn unser Flugzeug in der Luft ist (groundtrigger.triggered=0), und mit was anderem als mit seinen Rädern (das groundtrigger Primitive)
129. //den Boden berührt wird das als Crash gewertet.
130. //Wir müssen in diesem Fall die Geschwindigkeit zu einer Kraft machen damit wir das Flugzeug kollidieren lassen können
131.
132. //When our airplane is in the air (groundtrigger.triggered=0), and touches the ground with something different than
133. //the wheels (groundtrigger primitive) it will count as crash.
134. //We need to convert the speed into a force so that we can let our airplane collide
135.
136. function OnCollisionEnter(collision : Collision) {
137.     if (groundtrigger.triggered==0) {
138.     groundtrigger.triggered=1;
139.     crashforce= speed*10000;
140.     speed=0;
141.     gameover=1;
142.     rigidbody.useGravity = true;
143.     }
144. }
I had too split it to 3 posts because the code was too long...

2. ### legand

Joined:
Jan 22, 2011
Posts:
371
LOL you put your assets in there. now someone could steal it

3. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
Assets? Do you mean the airplane and the instrument graphics from my file? They are meant to be public. They are freeware, like all of my graphics

mr_snake, have a look at your third post, have a look at the code. There you can read :

//-------------------------------------------------- Limiting to playfield --------------------------------------------------------------------------

the few lines below is the code you are looking for.

4. ### Rush-Rage-Games

Joined:
Sep 9, 2010
Posts:
1,998
How did I miss that?!?! Thanks a lot, this has been a great help!

5. ### B-Nix

Joined:
Feb 27, 2011
Posts:
3
Thanks a lot for sharing the results of your hard work, Tiles. This is really helpful to me just get started with Unity.

One thing I noticed though is that in level flight with the throttle untouched the plane gradually loses height. Is this intentional? How can i make the plane remain at the same height as long as it is in level flight?

6. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
Totally overlooked this question. From what i remember this is some kind of a rounding error somewhere. I have no idea how to fix that, sorry. Haven`t touched this code since several months, and the project is abandoned.

7. ### rockysam888

Joined:
Jul 28, 2009
Posts:
650
Hi,

What is the last updated version?

8. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
Build 61, plus the joypad fix.

I´ve edited the first posting so that it contains all links ot all files, and have removed those links from the postings below. So all file links are in the first posting now.

9. ### normalmaps

Joined:
Feb 15, 2011
Posts:
48
How do i prevend the plane from going down, when decelerating ?

10. ### normalmaps

Joined:
Feb 15, 2011
Posts:
48
I got it, for the ones who want to know set Uplift = 0

Joined:
Feb 5, 2010
Posts:
2,475
Good find

12. ### sushanta1991

Joined:
Apr 3, 2011
Posts:
289
@Tiles
First of all i would like to say thanks for your great help, now i am understanding the concept of the plane.
you use groundtrigger.triggered in your script, this thing is creating some confusion for me I am not understanding the concept to use groundtregger could you please help me to understand what it is.

13. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
I call a variable from another script that way. I could also say i point to it. The script is called " groundtrigger.js ". The variable that i want to call in this script is called " triggered ". The dot between the two words is something like a slash in a path, which leads to our variable in another script. But instead a slash a dot gets used.

The groundtrigger.js script checks if the airplane has ground contact.

14. ### sushanta1991

Joined:
Apr 3, 2011
Posts:
289
thanks for giving your time to me, now i got it. Still there are many more Questions on your script if you give me permission to ask then i can proceed because it is important for me to learn some flight script.

15. ### yaccbr

Joined:
Jul 12, 2011
Posts:
24
Hello

I'm very new to Unity and to this comunity, so please apologize me if I make some kind of mistake or broke some etiquette... just explain to me and I will be glad to correct anything.

For my start with Unity I choose the Tiles example of a ligth and simple arcade flight to learn things..

I heavily modified the script to give more freedom of movements and use mouse as the input controller for the "aircraft"... my little son enjoeyd very much the felling and experience on this early stage... very early stage, so it turns to be my starting project to complete a small flight sim aimed for kids.. with missions, combats and race like levels...

Here is the link for the web demo, test, pré-alpha...: http://www.yaccbr.xpg.com.br

The HUD is in portuguese so a little translation here: chose cameras pressing 1 or 2 or 3... f2 restart the level (as in the original script), use the mouse to control the airplane...

Any feedback will be apreciatte...

Thanks Tiles for sharing your work... as soon as possible, with your permission I could show here the changes I made?

Last edited: Aug 2, 2011
16. ### Rush-Rage-Games

Joined:
Sep 9, 2010
Posts:
1,998
Personally, I don't like the new controls...

17. ### coin-god

Joined:
Jan 21, 2010
Posts:
325
It's very hard to control, I tried the arrow keys and couldn't get the plane straight again.

18. ### yaccbr

Joined:
Jul 12, 2011
Posts:
24
Thank's for the fast feedback!

It's hard to control? Sugestions? Make the turn rate slower?

I'd adjust it to simulate the tradicional flight sim feel.. like the olds Warbirds from IEN or Aces High.. and it's no so diferent form Flight Simulator form MS... but I'm a hardcore flightsim player... I'm very use to its control feeling... it's hard to think in another way..

Please, if you could point some directions..

19. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
This little project was a giveaway. Do whatever you want with it. I would nevertheless suggest to open another thread for it. It could become a mess when there are too much modifications in one thread around

20. ### RoDester

Joined:
May 21, 2011
Posts:
64
Wow nice build! but a sugestion how could I add so that the plane can go like do a barrel roll? like those of yaccbr build ?

21. ### Rush-Rage-Games

Joined:
Sep 9, 2010
Posts:
1,998
I would like to know how to do this too.

Would you use a animation? Or just do it through code?

22. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
It was never my goal to implement a barrel roll. And i am still lost how to implement it here. I haven`t touched this code since a year or so. So i am of no big help anymore anyways, sorry. I fear you are at your own here

23. ### yaccbr

Joined:
Jul 12, 2011
Posts:
24
Hi RoDester... I ripped off the code the constrains of the motion control... so it's controllable like a real airplane ... well without the yaw and all the rough math for a real simulation... so you will have the 6 degrees of freedom on the control

As soon as possible I will paste here (or in the other tread) the modified code, my computer was fried by a short circuit and will be back in the next 20 days... I'm praying to have the HD content intact...

24. ### Baseone

Joined:
Aug 30, 2011
Posts:
6
Schön gemacht Tiles

Joined:
Feb 5, 2010
Posts:
2,475
Danke

26. ### rohan bhangui

Joined:
Oct 4, 2011
Posts:
3
okay i have been using your scripts and things try it out.....i can't get one thing to work......: how did you get the guis to remain in one spot.....and how can one code a background such that the other guis would be relative in terms of position to the background

27. ### Fernis

Joined:
May 12, 2012
Posts:
6
Hi Tiles.

I'm working on a game where the player controls an air plane via kinect and sound, so I went through the web and found your flight simulator which was the best one I could find. I have modified your 'moveairplane' script and tried to apply your physics system to the air plane (hope it's all right with you ), but the plane just falls through the terrain. I have made the three sensors and the groundtrigger and applied your scripts to each object and I can see that the front sensor gets triggered when it hits the terrain. Can you please tell me whether there is something specific I need to be aware of when I apply your physics system?

28. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
You can do with that code whatever you want. It`s freeware and open source without any restrictions or license. But i fear it is not really the best flight simulation code around. I once made this project to learn the first steps in Javascript. And the movement is not physics based. Which can lead to some unexpected quirks when the valuebased movement battles with the physics system.

For your problem, i have absolutely no clue why your airplane falls through the terrain, sorry. I haven`t touched this code since years. And i don`t know your setup. I fear you have to fix that one by yourself.

Joined:
May 12, 2012
Posts:
6

30. ### parjanyaroy

Joined:
Oct 14, 2012
Posts:
4
awesome game ... was looking for something like dis... will help me to learn loads of stuff .... have downloded BUILD 18 to grasp the basic concepts . Once i understand them fully then i will go for the newer updates. Thnx a ton ..

Last edited: Oct 14, 2012
31. ### parjanyaroy

Joined:
Oct 14, 2012
Posts:
4
I was also experimenting with the controls on a duplicate object.d same thing was happening untill i UNCHECKED the GRAVITY option of the new object i made.Maybe dat'll solve ur problem.m a complete newbie ovr here

Joined:
Apr 6, 2012
Posts:
10

33. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
Whoopsie, Seems that there are some very old links left that leads to the old domain. I will have a look to fix them.

EDIT; fixed. Links should work again

Last edited: Dec 8, 2012
34. ### aidengaming123

Joined:
Apr 19, 2013
Posts:
55
How would you make this mobile? Any ideas...

35. ### kevinv321

Joined:
Jul 8, 2013
Posts:
2
Hi all, this is a very cool project!

36. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
Thanks Kevin. I would make things a bit different today though

aidengaming123, what do you mean to make it mobile? Simply export it in a mobile format. iOS or Android. Unity is multiplatform.

Joined:
Dec 1, 2009
Posts:
2,836
38. ### etmgames

Joined:
Apr 12, 2014
Posts:
11
any mobile version?

39. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
There is no mobile, windows, mac, linux or anything else version. Just a code snippet that can be exported to any of those platforms. Unity is multiplatform, you know

And no, this is not longer maintained. Use and modify it as is.

Joined:
Sep 6, 2016
Posts:
1

41. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
Odd that the links in the first page doesn't work anymore. They are the same than the ones from my project page. I can unfortunately not edit this old post anymore. So use this ones:

http://www.reinerstilesets.de/anderes/unity-testproject/

But note that this code is stoneold, and not longer maintained.

42. ### BIO_K

Joined:
Feb 3, 2016
Posts:
5
Hello this is amazing I love it... I just have one problem... when I load it in a different map... The plane crash at start... Why is that how should I align the sensors with the ground? Whats up with that? sorry my Java sucks a lot!

43. ### Tiles

Joined:
Feb 5, 2010
Posts:
2,475
I dunno. I haven't touched this for years. Please don't use this old code anymore. It is completely outdated nowadays

unityunity