Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by DiscoFever · Mar 01, 2015 at 09:59 AM · 2dflightflight sim

Need help with 2D Flight Simulator

Hi,

I'm trying to adapt the following 3D Flight sim to 2D: http://forum.unity3d.com/threads/flight-sim-starter-script-files-included.42616/

which i have translated to 2D and C# (well i hope so)

I create a simple object, with RigidBody2D but guess.. nothing happens when i hit jump; i want the plane to have some 'trust' ...

Can some gentleman help me out there ?

Here's what i've got so far ...

 usingUnityEngine;
 usingSystem.Collections;
 
 publicclassFly : MonoBehaviour {
 
 publicfloatthrottleDelta = 0.08f; //Thisdefineshowfastthethrottlevaluechanges
 publicfloataccelerateConst;
 publicfloatdecelerateConst = 0.065f; //Ifoundthisvaluegivessemi-realisticdeceleration, changeasyouseefit.
 publicfloatmaxSpeed = 100f;
 publicfloatspeedConst = 100f;
 publicfloatthrottleConst = 30f;
 publicfloatairbrakeConst = 30f;
 
 publicfloatliftConst; //Anotherarbitraryconstant, changeitasyouseefit.
 publicfloatdragConst; //NotethatthisisNOTthesameastherigidbodydrag. Thisisusedfortheairbrake
 publicfloatangleOfAttack; //Effectiverange: 0 <= angleOfAttack <= 20
 publicfloatgravityConst = -9.81f; //Anarbitrarygravityconstant, thereisnoparticularreasonithastobe9.8...
 publicfloatmaxDiveForce ;
 publicfloatnoseDiveConst;
 publicfloatminSmooth;
 publicfloatmaxSmooth;
 publicfloatmaxControlSpeedPercent = 75f; //Whenyourspeediswithentherangedefinedbythesetwovariables, yourship'srotationsensitivityfluxuates.
 publicfloatminControlSpeedPercent = 25f; //Ifyoureachthespeeddefinedbyeitherofthese, yourshiphasreachedit'smaxorminsensitivity.
 
 //Adding
 publicfloatangleOfIncidence, throttle;
 publicfloatpitch, roll, yaw;
 publicintlevelFlightPercent;
 
 publicboollockedConst;
 publicintrotationConst;
 publicintpitchConst = 50;
 publicintrollConst = 50;
 publicintyawConst = 15;
 
 //Thesesetthesensitivityofeachindividualaxis. Ifyouwanthighersensitivityonpitchforexample, changethisvalue.
 publicfloatpitchDelta = 1.05f;
 publicfloatrollDelta = 1.05f;
 publicfloatyawDelta = 1.05f;
 
 //AirplaneAerodynamics - Istronglyreccomendnottouchingthese...
 publicfloattrueSmooth;
 publicfloatsmoothRotation;
 publicfloattruePitch;
 publicfloattrueRoll;
 publicfloattrueYaw;
 publicfloatthrust;
 publicfloattrueThrust;
 publicfloattrueDrag;
 publicfloatnosePitch;
 publicfloatattitude;
 
 //Usethisforinitialization
 voidStart () {
 smoothRotation = minSmooth + 0.01f;
 if (lockedConst == true)
 {
 pitchConst = rotationConst;
 rollConst = rotationConst;
 yawConst = rotationConst;
 Cursor.visible = false;
 }
 }
 
 //Updateiscalledonceperframe
 voidUpdate () {
 
 //CalculatingAngleofAttack
 attitude = -(Vector2.Angle(Vector2.up, transform.forward)-90);
 angleOfIncidence = attitude - angleOfAttack;
 
 pitch = 0f * pitchConst;
 
 
 pitch *= pitchDelta * Time.deltaTime;
 
 //SmothingRotations...
 if ((smoothRotation > minSmooth)&&(smoothRotation < maxSmooth)) {
 smoothRotation = Mathf.Lerp (smoothRotation, trueThrust, Time.deltaTime);
 }
 
 if (smoothRotation <= minSmooth) {
 smoothRotation = smoothRotation + 0.01f;
 }
 
 if ((smoothRotation >= maxSmooth) &&(trueThrust < (maxSpeed*(maxControlSpeedPercent/100f)))) {
 smoothRotation = smoothRotation -0.1f;
 }
 
 trueSmooth = Mathf.Lerp (trueSmooth, smoothRotation, 5f* Time.deltaTime);
 truePitch = Mathf.Lerp (truePitch, pitch, trueSmooth * Time.deltaTime);
 
 
 if (Input.GetButton ("Jump")) {
 throttle = 2130f;
 } else {
 throttle = 0f;
 }
 
 
 if ( throttle/speedConst >= trueThrust)
 {
 trueThrust = Mathf.SmoothStep (trueThrust, throttle/speedConst, accelerateConst * Time.deltaTime);
 }
 if (throttle/speedConst < trueThrust)
 {
 trueThrust = Mathf.Lerp (trueThrust, throttle/speedConst, decelerateConst * Time.deltaTime);
 }
 
 // * * Nowweareapplyinglift, gravityandlevelflighttoairplane.
 GetComponent<Rigidbody2D>().drag = liftConst*((trueThrust)*(trueThrust));
 trueDrag = dragConst*((trueThrust)*(trueThrust));
 
 if (trueThrust <= (maxSpeed/levelFlightPercent))
 {
 
 nosePitch = Mathf.Lerp (nosePitch, maxDiveForce, noseDiveConst * Time.deltaTime);
 }
 else
 {
 nosePitch = Mathf.Lerp (nosePitch, 0, 2* noseDiveConst * Time.deltaTime);
 }
 }
 
 
 voidFixedUpdate () {
 //SeperatedaddRelativeForcesowehavebettercontroloverwhenwewantthemtorun.
 if (trueThrust <= maxSpeed)
 {
 //HorizontalForce
 GetComponent<Rigidbody2D>().AddRelativeForce (newVector2(0f, (trueThrust - trueDrag)*5f));
 transform.Translate(0,0,(trueThrust-trueDrag));
 }
 
 GetComponent<Rigidbody2D>().AddForce (newVector2 (0,(GetComponent<Rigidbody2D>().drag - gravityConst)), ForceMode2D.Force);
 
 transform.Rotate (truePitch,-trueYaw,trueRoll);
 Debug.Log (GetComponent<Rigidbody2D> ().drag - gravityConst);
 GetComponent<Rigidbody2D> ().AddTorque (nosePitch / 200f);
 
 }
 }
 [/code]
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Flight HUD - flight angles 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Flight physics 0 Answers

JSBSim and Unity 0 Answers

Realistic 2D flight physics? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges