Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Realistic Flight Simulator on Unity

Discussion in 'General Discussion' started by IvanRehder, Oct 20, 2023.

  1. IvanRehder


    Mar 14, 2020
    Hello all,

    Me and my friend are creating a Realistic Flight Simulator on Unity. We are basing our project on classes that we took on how to simulate aircrafts on MatLab and we decided to challenge ourselves to build one on Unity

    Currently we are only caring about the aircraft model itself, so we do not know yet which challenges we are about to face when we add the world in the scene.

    Since this is my first thread I'm not sure which are the good methods to post code and scripts doubts/problems here, so any good advice will help.

    Also currently we are with problems about the integration of the forces and moments in our aircraft.

    Can anyone with experience on this kind of game/simulator help us?

    [Here I should add some code or images about our project, but I don't know how, since it has many script files. Again, any tip will help us]

    Thank you all
  2. DragonCoder


    Jul 3, 2015
    To post codes, there are two options in the bar of the text editor: upload_2023-10-20_19-23-38.png

    Would say a realistic flight simulator is well possible in Unity and sounds like an interesting way to put your knowledge to use too.
    Is it the first time you touch any game engine though? In that case in can be a bit rough since that on its own has a learning curve.
    The physics system is likely simpler to use than what Matlab Simulink can do however. Don't expect perfect determinism because game engines are optimized for performance over realism.

    By the way, actual code questions rather belong in here:
    But general questions on algorithms to use may be tolerated here as well like this thread of yours.

    It takes a little bit to understand properly what all the force options and methods are that you have with Unity's Rigidbody component. Use Unity 2022.3 or newer since they added the GetAccumulatedForce/Toqrque methods which can be helpful for debugging physics stuff.
    I recommend making a simple test bed where you just try out the methods with simple controls to get familiar with them.
    Note that the physics system does have an InertiaTensor you can set:
    Unity will calculate one on its own using the colliders, but you will likely want to define your own for realism of something complex like a plane.

    My experience resumes to a 2D underwater game where water friction plays a role, as well as an old college project trying to have a system that teaches itself flying with wing flaps.
    So a few ideas come to mind:

    You have two basic options to branch into, if you ask me:
    A. Have a virtual physics model of your plane that calculates the forces applied to the plane.
    B. Use a highly simplified air-drag model and physically acting flaps, elevators and rudders etc. on the plane.

    In case of A, your plane in Unity is, as far as physics are concerned, just one single game object with a rigidbody component. That one allows you to apply forces to it. Your algorithm needs to determine what torque and forces would be applied when the player turns the yoke or applies other controls.
    This means you model the whole physics yourself except for the basic inertia model of the rigidbody. Advantage of this is that this is likely way easier parametrizable, but realism may be harder to achieve and you need to redo much of the parametrization on every plane that is vastly different.

    For B. I'd suggest that you model the plane in such a way that all aerodynamically movable parts of the plane are separate objects (without rigidbodies) and child elements of the core rigid body.
    The player should then control the real angle of all these objects like they would in a plane.
    To actually compute what forces the rigibody needs to receive due to those flaps, you then need to calculate the flap-surface that is relative to the movement direction -> What you need is how much air was dispersed in what direction since the last frame. Translate this to a local force on the flap (split very large flaps into multiple) and then transpose that to the force applied to the core rigid body.
    This is the technique I used for the wings of my AI project.
    That alone does not represent the uplift of the main wings which you would need to simulate separately (including effects like stalling), but it should be sufficiently realistic for the flaps etc. and ideally this should transfer smoothly between different plane sizes and shapes to allow you to design the plane physics visually instead of purely setting numeric parameters.
    Naturally this also ignores effects like drag from air vortices, but going this far with realism will stretch your project quite far. Get the plane flying without that first :)

    Alternative B.b: Give the flaps their own rigidbodies (without being child objects of the core!!) and connect them all via joints to the core. This is cool for a game as it'd allow obstacles to hit the wings and stuff like that, but I fear this can become unstable if you add as many flaps as a large plane has (joints can be a pain in that regard).

    P.s. to make a cockpit you can use a technique called camera stacking.

    By the way, hope this is not demotivating, but there are at least two solid seeming assets that go in this direction: [Note that this one does not seem to include the source code!]
    Last edited: Oct 20, 2023
    Noisecrime likes this.
  3. Deleted User

    Deleted User


    Don't waste your time, I did that for you..10 years!
    Flight Projects I Never Published.

    I ended up taking a class on flight physics form Udemy and rolled my own flight mechanics.
    Unity does not have the terrain system that will work with flight simulators,,period.
    So you will have to craft a realistic mesh terrain and shaders,,

    I actually quit Unity over these problems, never getting fixed., so my advice is..
    DON"T. DO IT
    devotid, koirat and Ryiah like this.
  4. Ryiah


    Oct 11, 2012
  5. neginfinity


    Jan 27, 2013
    Unity is a single-precision engine, meaning you have roughly +- 8km cube around zero coordinates where everything works fine. You also could bypass that through trickery and by handling calculations yourself. I messed with this before.
    And this also applies to physics. And in case of flight sim you might need to roll your own solution for the plane anyway (even though you technically can apply forces to rigidbodies)

    I think you'll have easier time if you use engine that supports double coordinates out of the box. Unigine can do that, Unreal has that as a beta feature.

    It only matters if you want life-sized earth, life-sized distances and aren't writing an RC plane or drone sim. GTA series had aviation and the entire world fits into single precision limits. Things are different if you're trying to make your own X-plane where you can fly across planet in real time.

    For double precision physics see Bullet. I think it could be compiled with double precision.
  6. mgear


    Aug 3, 2010
    for examples, github has multiple flight physics repos (could be interesting to check for inspiration/reference)
    like this one and many others (couldn't find them right now..)
  7. DragonCoder


    Jul 3, 2015
    For a game where everything always happens near the player character, meaning no NPCs or multiplayer, implementing a floating origin is essentially trivial:
    Wouldn't see that as a blocker.

    Space flight is a whole other beast of precision requirement. You won't have that scaling issue of transfering between planetary and galactic coordinates in a plane game.

    Unity lacking a dynamic terrain solution built-in is a bigger point, however the TE should say if that is really their goal or they "only" wanna focus on putting what they learn on avionics to use. It didn't sound like they wanna make a full fledged game to sell.
    A nice, endless ocean is available for Unity as well to allow for a coherent looking environment. You could even model some large islands with Unity's built-in terrain for diversity and randomly spawn them in as obstacles.

    Unreal engine may have those terrain features, but translating matlab equations to blueprint is likely a bigger hassle than to C#.
    Last edited: Oct 21, 2023
    Antypodish likes this.
  8. Ryiah


    Oct 11, 2012
  9. neginfinity


    Jan 27, 2013
    Flight sim is not exactly such game, though. Things can happen far away from player, and depending on flight speed you are very likely to compute everything in doubles. Basically, for example DCS world has maps with size of 500 x 500 kilometers and has supersonic jets.

    While it is not a blocker, it can quickly turn into a major annoyance that's best avoided if you can do that.