Guest User

Untitled

a guest
Sep 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4.  
  5. public class ArianplaneController : MonoBehaviour {
  6.     public float maxSustentation = 9.81F;
  7.    
  8.     public Transform centerOfMass;
  9.    
  10.     public Transform alaDerecha;
  11.         public bool alaDerechaActiva = true;
  12.    
  13.     public Transform alaIzquierda;
  14.         public bool alaIzquierdaActiva = true;
  15.    
  16.     public ArianplaneWheels wheels;
  17.     public float raycastDistance = 0.3F;
  18.    
  19.     private bool isOnTheFloor = true;
  20.    
  21.     // Debug variables
  22.         private bool _debugMode = false;
  23.    
  24.     void Start() {
  25.        
  26.     }
  27.    
  28.     // GUI shit
  29.         void OnGUI() {
  30.             if (this._debugMode)
  31.                 this._Debug();
  32.         }
  33.    
  34.         private void _Debug() {
  35.             GUI.BeginGroup(new Rect(10, 10, 200, 300));
  36.             GUI.Box(new Rect(0, 0, 200, 300), "Debug information");
  37.             GUI.Label(new Rect(5, 20, 200, 30), "Is on the floor: "+this.isOnTheFloor);
  38.             GUI.EndGroup();
  39.         }
  40.    
  41.     void Update() {
  42.         if(Input.GetKeyDown(KeyCode.F2))
  43.             this._debugMode = !_debugMode;
  44.        
  45.         _isOnTheFloor();
  46.     }
  47.    
  48.     void FixedUpdate() {
  49.         rigidbody.centerOfMass = centerOfMass.localPosition;
  50.        
  51.         if (alaDerechaActiva)
  52.             rigidbody.AddForceAtPosition(new Vector3(0, maxSustentation/2 * rigidbody.mass, 0), alaDerecha.position);
  53.         if (alaIzquierdaActiva)
  54.             rigidbody.AddForceAtPosition(new Vector3(0, maxSustentation/2 * rigidbody.mass, 0), alaIzquierda.position);
  55.        
  56.  
  57.     }
  58.    
  59.     private void _controls() {
  60.        
  61.     }
  62.    
  63.     private bool _isOnTheFloor() {
  64.         bool one = false;
  65.         bool two = false;
  66.         bool three = false;
  67.        
  68.         Debug.DrawRay(wheels.frontLeft.position, -wheels.frontLeft.up, Color.blue, 0.01F);
  69.         if(Physics.Raycast(wheels.frontLeft.position, -wheels.frontLeft.up, raycastDistance)) {
  70.             one = true;
  71.         } else {
  72.             one = false;
  73.         }
  74.        
  75.         Debug.DrawRay(wheels.frontRight.position, Vector3.down, Color.blue, 0.01F);
  76.         if(Physics.Raycast(wheels.frontRight.position, Vector3.down, raycastDistance)) {
  77.             two = true;
  78.         } else {
  79.             two = false;
  80.         }
  81.        
  82.         Debug.DrawRay(wheels.back.position, Vector3.down, Color.blue, 0.01F);
  83.         if(Physics.Raycast(wheels.back.position, Vector3.down, raycastDistance)) {
  84.             three = true;
  85.         } else {
  86.             three = false;
  87.         }
  88.        
  89.         if (one && two && three) {
  90.             this.isOnTheFloor = true;
  91.             return true;
  92.         } else {
  93.             this.isOnTheFloor = false;
  94.             return false;
  95.         }
  96.        
  97.     }
  98.    
  99.     /*private float elapsed;
  100.    
  101.     // Use this for initialization
  102.     void Start () {
  103.    
  104.     }
  105.    
  106.     // Update is called once per frame
  107.     void Update () {
  108.         UpdateForces();
  109.     }
  110.    
  111.     //************************************************************
  112.     // Use PhysX rigidbody to update forces acting on a plane
  113.     //************************************************************
  114.     public void UpdateForces()
  115.     {
  116.         elapsed += Time.deltaTime;
  117.    
  118.         //************************************************************
  119.         // Update Roll & Yaw
  120.         //************************************************************
  121.         float ax = 0;
  122.         float ay = 0;
  123.    
  124.    
  125.         //************************************************************
  126.         // Accelerometer controls, cater to specific situations
  127.         //************************************************************
  128.         if ( FlightStick.gameObject.active )
  129.         {
  130.    
  131.              torqueY = 0.1f * FlightStick.NormalizedX * FlightManager.Data.Handling;
  132.    
  133.              torqueX = 400 * FlightStick.NormalizedY * (1-FlightManager.Data.Stability);
  134.    
  135.            EngineThrust += 25 * ThrustStick.NormalizedY;
  136.            if( EngineThrust <= 0 )
  137.            {
  138.              EngineThrust = 0;
  139.            }
  140.            else if ( EngineThrust >= MaxThrust )
  141.            {
  142.              EngineThrust = MaxThrust;    
  143.            }    
  144.         }
  145.    
  146.         //************************************************************
  147.         //4 Major forces on Plane: Thrust, Lift, Weight, Drag
  148.         //************************************************************
  149.         Vector3 airSpeedVector = -rigidbody.velocity;
  150.    
  151.         float angleOfAttack = -Mathf.Deg2Rad * Vector3.Dot( rigidbody.velocity, transform.up);
  152.         float slipAoA = -Mathf.Deg2Rad * Vector3.Dot( rigidbody.velocity, transform.right);
  153.         float speedWeight = AirDensity * ((CurrentSpeed*CurrentSpeed)/2);
  154.         float weightCo = Weight;  
  155.    
  156.         sideSlipCoefficient = 0.001f * slipAoA * speedWeight;
  157.         liftCoefficient = WingArea * angleOfAttack * speedWeight;
  158.    
  159.         //************************************************************
  160.         //drag goes from x -> 3x * %
  161.         //angle goes from -1 -> 1
  162.         //************************************************************
  163.         float horizonAngle = Vector3.Dot( transform.forward, Vector3.up);
  164.         dragCoefficient = 2*AirDrag + 2 * AirDrag * horizonAngle;
  165.         thrustCoefficient = EngineThrust;
  166.         AoA = Vector3.Dot( transform.up, Vector3.up);
  167.    
  168.    
  169.         //************************************************************
  170.         // Pitch from lift, yaw & roll from sideslip
  171.         //************************************************************
  172.         rollCoEfficient = -(torqueY+ay) * RollRate * speedWeight;
  173.         pitchCoEfficient =  (torqueX+ax) * ElevatorRate * speedWeight;
  174.         yawCoEfficient = 0;
  175.    
  176.         pitchCoEfficient += liftCoefficient * 0.001f;
  177.         yawCoEfficient += -sideSlipCoefficient * 2f;
  178.         rollCoEfficient += -sideSlipCoefficient * 0.1f;
  179.    
  180.         //************************************************************
  181.         // Calc vectors now we have coefficients
  182.         //************************************************************
  183.         WeightVector = Vector3.down * weightCo;
  184.    
  185.    
  186.         ThrustForceVector = transform.forward * thrustCoefficient;    
  187.         LiftVector = transform.up * liftCoefficient;
  188.         DragVector = airSpeedVector * dragCoefficient;
  189.         SideSlip = Vector3.right * sideSlipCoefficient;
  190.    
  191.         RollTorque = Vector3.forward * rollCoEfficient;
  192.         YawTorque = Vector3.up * yawCoEfficient;    
  193.         PitchTorque = Vector3.right * pitchCoEfficient;
  194.    
  195.         //************************************************************
  196.         // Apply forces
  197.         //************************************************************
  198.         rigidbody.AddForce(ThrustForceVector, ForceMode.Force);
  199.         rigidbody.AddForce(DragVector, ForceMode.Force);
  200.         rigidbody.AddForce(LiftVector, ForceMode.Force);
  201.         rigidbody.AddForce(WeightVector, ForceMode.Force);
  202.         rigidbody.AddForce(SideSlip, ForceMode.Force);
  203.    
  204.         rigidbody.AddRelativeTorque(RollTorque);
  205.         rigidbody.AddRelativeTorque(YawTorque);
  206.         rigidbody.AddRelativeTorque(PitchTorque);
  207.    
  208.         //************************************************************
  209.         // Instrumentation data & debug output
  210.         //************************************************************
  211.         VelocityVector = rigidbody.velocity;
  212.    
  213.         PlaneLift = liftCoefficient;
  214.         //AoA = angleOfAttack;
  215.         RotX = transform.rotation.eulerAngles.x;
  216.         RotY = transform.rotation.eulerAngles.y;
  217.         RotZ = transform.rotation.eulerAngles.z;
  218.         torqueX = 0;
  219.         torqueY = 0;
  220.         CurrentSpeed = VelocityVector.magnitude;
  221.         CurrentFuel -= Time.deltaTime * Weight/5 * EngineThrust/MaxThrust * 0.3f * FuelConsumption;
  222.    
  223.     }*/
  224. }
  225.  
  226. [Serializable]
  227.  
  228. public class ArianplaneWheels {
  229.     public Transform frontLeft;
  230.     public Transform frontRight;
  231.     public Transform back;
  232. }
Add Comment
Please, Sign In to add comment