• Stars
    star
    625
  • Rank 71,455 (Top 2 %)
  • Language
    C#
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

War Thunder style mouse flight controls for aircraft

Mouse Flight v1.3

This project is an example of mouse controls for aircraft based on the excellent system used by Gaijin's War Thunder. It is in my opinion the single best and most robust system for controlling aircraft, especially those with more realistic physics.

screenshot

This project was built in Unity 2017.3.1f1, and tested in 2018.3.0 and 5.6.4.

Note: When importing the package into 5.6.4, the demo scene will need to have its lighting reset. (Open Window->Lighting->Settings, then click the gear icon on the top right -> Reset.)

Download

You can either clone the repository or download the asset package located in the root.

Demo Controls

Demo plane

To fly the demo plane, simply move the mouse around. The circle is controlled by the mouse, while the crosshair shows where the plane is pointed. The plane will automatically try to fly towards the crosshair.

Sometimes you might want to manually control the pitch or roll for a maneuver, so a couple keyboard overrides exist.

  • W/S: Pitch up/down
  • A/D: Roll left/right
  • C: Enable free look

Usage

The core of this project is the MouseFlightController component and the MouseFlightRig prefab.

  1. Drop the MouseFlightRig prefab into your scene
  2. Assign your vehicle's transform to the Aircraft property in the MouseFlightController's Inspector
  3. In code, use the MouseFlightController.MouseAimPos C# property to get the world postion the mouse cursor is pointing to
  4. Make your vehicle fly to that position
  5. Use the MouseFlightController.MouseAimPos and MouseFlightController.BoresightPos C# properties to draw HUD elements for the mouse cursor and aircraft's direction respectively

The camera rig only handles telling where the aircraft is meant to fly. How your aircraft actually flies towards that point is an implementation detail specific to your project and will require scripting. The same goes for the HUD elements.

To give an example of how you might do this, the demo includes a very simple Rigidbody based airplane with the steering behaviors necessary to fly itself. An extremely simple HUD is also included to demonstrate how it's supposed to work.

Control Principles

The mouse is not an ideal method of controlling an aircraft. Most control schemes involve using the mouse as a substitute for a joystick or otherwise directly controlling the aircraft. While these can work, they don't take advantage of the mouse's greatest strength: as a pointing device.

What the control scheme in this project does is allow the player to use the mouse as a pointing device to fly the plane. If you want to fly towards something, you simply put the mouse cursor over it and the plane will fly towards it. If you want to aim at something, just put the mouse cursor over it.

Mouse Control Rig

Axes demonstrating the mouse aim transform

The simplest way to accomplish this idea is to have an orbit camera that gets rotated by the mouse, crosshair in the middle of the screen, and then wherever the camera is pointed is where the plane flies. While this definitely does work, it can be a bit clumsy and make for some jarring camera movement depending on your game. Examples of this style of control would be the Halo games or World of Warplanes.

Mouse flight rig

To get around this and emphasize the mouse's pointing ability, the camera and mouse control need to be separated. As can be seen in the expanded hierarchy of the rig prefab, one transform is rotated directly by the mouse and used to point towards a direction, while the camera transform smoothly looks in this direction using its own, separate rig. This separation allows for accurate pointing with the mouse while maintaining smooth camera motion. This is the method used by War Thunder.

Rig Transforms

Aircraft

The reference transform used for both the boresight position and for the rig to follow. You'll want to check the Use Fixed option depending on if the aircraft is moved in Update or FixedUpdate loops. Having this set wrong will result in jittery looking movement.

Mouse Aim

The heart of rig. This is an empty transform that is rotated directly by the mouse in order to generate the MouseAimPos. The MouseAimPos is the forward of this transform multiplied by the Aim Distance given in the inspector.

In order to keep the rotations intuitive, the rotations must happen relative to the camera. This way, when the user moves the mouse left, the apparent position of the MouseAimPos (and consequently your cursor) should also move left on the screen regardless of the current orientation.

Camera Rig

Used to rotate a camera to face the aim position. This is the typical orbit camera setup where a camera is put on a parent transform centered on the player, and then that transform is rotated. Normally, this rig is kept level with horizon (up vector is world's up). However, when the aircraft is flying straight up or down, this will cause problems. In those situations, the camera rig's Quaternion.LookRotations are generated using the rig's own up vector rather than the world's up.

If you'd like to add camera motion, such as the camera moving back when the aircraft accelerates, this is the Transform to modify.

Camera

Transform of the camera itself. Only used as reference for rotating the MouseAim transform relative to the screen.

Changelog

1.3 (May 14 2019)

  • Fixed bug where demo plane Roll property reported pitch instead of roll.

1.2 (May 11 2019)

  • Added free look enabled by holding the C key

1.1 (Jan 6 2019)

  • Converted project/package to 2017.3.1f1 for improved compatibility
  • Removed C# code that was dependent on .Net 4.6 for improved compatibility
  • Removed unused materials and variables

1.0 (Jan 4 2019)

  • Released

More Repositories

1

FreelancerFlightExample

Spaceship flight in a similar style to Freelancer
C#
270
star
2

SimpleWings

A simple, configurable aerodynamic wing that applies lift at its location. Includes flyable example plane.
C#
215
star
3

AceArcadeMissiles

Arcade style missile code, effect, and sound pack.
C#
112
star
4

GunTurrets2

Starting point for turrets that can be mounted on arbitrarily rotating things. More robust version of GunTurrets.
C#
99
star
5

ArcadeSpaceFlightExample

Simple example of arcade style space sim flight physics.
C#
93
star
6

TinyXWingClean

Tech demo for testing JSON-loaded game data
C#
71
star
7

GunTurrets

Starting point for turrets that can be mounted on arbitrarily rotating things.
C#
71
star
8

Ergo

Fly a little ship around with raylib.
C++
62
star
9

GunsAndBullets2

Guns and bullets template designed with simulation games in mind.
C#
56
star
10

ArcadeJetFlightExample

Example project featuring simple arcade flight.
C#
54
star
11

GunsAndBullets

Generic gun and bullets
C#
54
star
12

PhysicsTankExample

Physics based tank
C#
52
star
13

BillboardNebula

Billboard driven space nebula that you can fly in and around
C#
32
star
14

Warthog

WheelCollider driven fun little vehicle like Halo's Warthog.
C#
32
star
15

SundayChallenges

Collection of small prototype/code exploration projects
ShaderLab
32
star
16

MessageTickerUI

Very simple UI element for displaying messages on screen.
C#
31
star
17

UnityCommon

Assortment of commonly re-used tools and code for my Unity projects
C#
30
star
18

FloatingCockpit

Oddball workaround for floating point errors using a disconnected cockpit
C#
28
star
19

TinyCombatTools

Official tools to help add new assets to the game Tiny Combat Arena.
C#
16
star
20

CubeWars

Space shooter
C#
10
star
21

BeamCannon

Freespace 2 style charging beam cannons.
9
star
22

DCSBuildableFARPs

Proof of Concept DCS mission for dynamically "buildable" FARPs from which players can then spawn from.
Lua
4
star
23

FLTextureExporter

Tool for batch exporting all the textures from Freelancer
C#
4
star
24

brihernandez.github.io

Pages
2
star
25

DigitalAirCombat

Server side scripts to turn DCS into a more fast paced, forgiving, and approachable game.
Lua
2
star
26

MacroquadThrusters

Rust
1
star