- Home /
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