• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by TTZ · Jan 12, 2010 at 12:38 PM ·

First person flight controls

Hey there, I've seem some similiar qustions but can't get the answer i want. I've tried for the alst 45 minutes to get a script up for a FPS controller for a flight machine. Since im sort of new to this, could someone show me how it works and explain a little why this and why that? So far I've added the mesh colliders around my map and tried experimenting with whatever script code could work for flying around and giving it sensivity so it gets the feel of a real plane. - How do i get a FPS controller to fly? - How do i add sensivity?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
15

Answer by duck · Jan 12, 2010 at 01:32 PM

If you're aiming for the feel of an aeroplane, I would ditch the pre-packaged first person controller entirely. It's not suited to flight controls - it's specifically designed to act like a person walking around on the ground.

If you want your aeroplane (or flying creature, or flying superhero) to have a first-person view, simply parent your camera to the object, in the correct position for the view that you require (eg, inside the cockpit).

Instead, create a new script from scratch which implements a simplified version of the very basic essentials of flight. Make sure your aeroplane gameobject has a rigidbody, and for the script you will need to implement the following:

  • Use AddForce to apply a force in the plane's local forward direction. This is equivalent to the throttle - the power provided by the propellor or jet. Depending on how arcadey your game is, you could make this a constant force, or make it so the user can change the power during play.

  • A 'lift' force, which pushes the plane in its local up direction. The amount of lift is relative to its forward speed. Again, you'd use AddForce for this. To get the forward speed, you need to project the plane's rigidbody.velocity onto the plane's forward vector using Vector3.Project, and then get the magnitude of the result.

  • Banking controls. Banking is rotation around plane's local Z (forward), so tilts the plane left/right. You can approximate this in a simple manner by using AddTorque to affect the rotation of the plane's rigidbody.

  • Pitch controls. Pitch is rotation around the plane's local X, and so tips the nose up and down. Again, this can be approximated by applying torque to the plane's rigidbody just like banking.

  • Drag effect. You should increase the amount of drag and angularDrag on the plane in proportion to speed, to simulate the effect of air resistance.

For the banking and pitch controls, amount of torque should be proportional to the plane's forward speed (just like the lift force) - the faster you go, the more effect the plane's wing flaps have, and therefore the faster you turn. This also means when the plane isn't moving, it can't turn in any direction.

You might also want to implement rudder controls, which would basically be a repetition of the Banking/Pitch controls, but adding torque around the plane's local up axis. This type of rotation is sometimes called the Yaw or Slew of the plane.

Hope this is enough to get you started.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image duck ♦♦ · Apr 16, 2010 at 03:30 PM 0
Share

As requested, I'll be adding an example script to this answer soon.

avatar image fireDude67 · Nov 23, 2010 at 02:23 AM 0
Share

Don't bother, I've done it!

avatar image Max 4 · Apr 07, 2011 at 09:35 AM 0
Share

This is helpful, but I just can't figure out how to turn this data into a script.

avatar image Yllier · Apr 17, 2014 at 01:15 AM 0
Share

Can you please explain how to use Vector3.project in the way that you described? I am trying to make a simple glider and lift is a critical part of it. Thanks!

avatar image melkorinos · Feb 19, 2015 at 04:30 PM 0
Share

I managed to create this script from this post, not perfect but works well. The only weird thing is the lift booster but my plane wouldnt take of without it. Still take like 10 secs to take off :P

 //Add force from jet engine , set enginePower to 15000
                 if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space)) {
                         rb.AddForce (transform.forward  * enginePower);
                 }
 
                 //Add lift force ,  set liftBooster to 100 
                 lift = Vector3.Project (rb.velocity, transform.forward);
                 rb.AddForce (transform.up * lift.magnitude * liftBooster);
 
                 //Banking controls, turning turning left and right on Z axis
                 rb.AddTorque (Input.GetAxis ("Horizontal") * transform.forward * -1f);
 
                 //$$anonymous$$ch controls, turning the nose up and down
                 rb.AddTorque (Input.GetAxis ("Vertical") * transform.right );
                 
                 //Set drag and angular drag according relative to speed
                 rb.drag = 0.001f * rb.velocity.magnitude;
                 rb.angularDrag = 0.01f * rb.velocity.magnitude;
avatar image
3

Answer by vishesh · Feb 21, 2010 at 05:18 PM

hi, just a thought,i little script on this post would help a lot of people, as this is the page that turns up on google the most about unity flight controls,

please post a script her,it would clarify a lot of things, like the proportions of the forces to be applied n stuff... (although i have managed to do this,im not sure if its properly implemented,it would be nise to compare it to a more accurate example script..)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Elicoor · Feb 21, 2010 at 06:22 PM 0
Share

I second this! :)

avatar image duck ♦♦ · Apr 16, 2010 at 03:27 PM 0
Share

ok when I get some time I will add an example script

avatar image fireDude67 · Jul 04, 2010 at 07:47 PM 0
Share

i also second/third it

avatar image fireDude67 · Jul 31, 2010 at 06:55 AM 0
Share

never mind, i figure out how to do this, see my answer

avatar image
2

Answer by fireDude67 · Jul 31, 2010 at 07:03 AM

Okay this is how I do it for my space-flight system (no aerodynamics/flight physics)

function FixedUpdate() { // Turn var h = Input.GetAxis("Mouse X"); var v = Input.GetAxis("Mouse Y");

rigidbody.AddRelativeTorque(0, h 20, v 20); // Other way you may like //transform.Rotate(0, h 20, v 20); }

Also if transform.forward doesn't work try transform.right

Oh and if you want to be able to rotate:

    var rotateAmount = 5.0;
    function FixedUpdate() {
       if (Input.GetAxis("Horizontal") < 0) { transform.Rotate(rotateAmount, 0, 0); }
       else if (Input.GetAxis("Horizontal") > 0) transform.Rotate(-rotateAmount, 0, 0); }
    }

To rotate use left/right arrow keys or A/D.

This also works for Third Person controls

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by davedev · Jan 12, 2010 at 04:29 PM

Wow. Good answer. A point to clarify. An airplane turns more by banking than it does by using the rudder. So, your controls might make the plane turn when it banks or when there's a combination of bank and rudder inputs.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image duck ♦♦ · Jan 15, 2010 at 02:31 PM 1
Share

Thanks! (although strictly this ought to be added as a comment rather than a separate answer)

avatar image Nealeygraphics · Mar 01, 2018 at 08:48 AM 0
Share

Saw this. $$anonymous$$now this is old. Yet actually in commercial aviation it is taught to use the rudder for turning rather than banking. The forces along the wing as the rudder forces the wing forward increase the speed of the air increasing lift inducing the bank. This keeps the aircraft in a more level non-jarring flight path for commercial aviation. In aerobatics where I come from the rudder can be used on knife edge or 90 degree bank to keep the aircraft aloft for a time, or depending on the aircraft indefinitely... realism. Anyhow, found this cause looking for input on how to change the degree the surfaces move in the newer standard assets aircraft. Can't find anything so far. Doesn't really change then goes complete 180 anything past 120. Thinking dealing with the limits of the stock animation tho. Back to looking :)

avatar image
1

Answer by TTZ · Jan 12, 2010 at 10:41 PM

Thanks a bunch guys, really helped me get started with this!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image duck ♦♦ · Jan 15, 2010 at 02:32 PM 1
Share

You're welcome! (although thanks should be added as a comment to the relevant question rather than as a separate new answer). Please click the tick to accept the answer if you feel your question is resolved.

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Material doesn't have a color property '_Color' 4 Answers

How to import the object from server to unity 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges