Advertisement
Guest User

SpacecraftControl.cs

a guest
Apr 18th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. [RequireComponent(typeof(Rigidbody))]
  6. public class SpacecraftControl : MonoBehaviour {
  7.     public float MaxEnginePower = 40f; // Maximum output of our engine.
  8.     public float RollEffect = 50f; // The strength of the effect for rolling.
  9.     public float PitchEffect = 50f; // The strength of the effect for climbing/diving.
  10.     public float YawEffect = 0.2f; // The strength of the effect for the horizontal turn.
  11.     public float BankedTurnEffect = 0.5f; // Then strength of the effect for banking.
  12.     public float AutoTurnPitch = 0.5f; // Value to correct pitch when not climbing/diving.
  13.     public float AutoRollLevel = 0.1f; // Value to correct roll when not rolling.
  14.     public float AutoPitchLevel = 0.1f;
  15.     public float AirBrakesEffect = 3f;
  16.     public float ThrottleChangeSpeed = 0.3f;
  17.     public float DragIncreaseFactor = 0.001f;
  18.  
  19.     private float Throttle;
  20.     private bool AirBrakes;
  21.     private float ForwardSpeed;
  22.     private float EnginePower;
  23.     private float cur_MaxEnginePower; // Not used in current script.
  24.     private float RollAngle;
  25.     private float PitchAngle;
  26.     private float RollInput;
  27.     private float PitchInput;
  28.     private float YawInput;
  29.     private float ThrottleInput;
  30.  
  31.     private float OriginalDrag;
  32.     private float OriginalAngularDrag;
  33.     private float AeroFactor = 1f;
  34.     private bool Immobilized = false; // Can use this to freeze the spaceship to override the controls.
  35.     private float BankedTurnAmount;
  36.     private Rigidbody rigidBody;
  37.     WheelCollider[] cols;
  38.  
  39.     // Use this for initialization
  40.     void Start () {
  41.         rigidBody = GetComponent<Rigidbody> (); // Get  reference to our rigidbody.
  42.         OriginalDrag = rigidBody.drag;
  43.         OriginalAngularDrag = rigidBody.angularDrag;
  44.  
  45.         for (int i = 0; i < transform.childCount; i++) {
  46.             foreach (var componentsInChild in transform.GetChild(i).GetComponentsInChildren<WheelCollider>()) {
  47.                 componentsInChild.motorTorque = 0.18f;
  48.             }
  49.         }
  50.     }
  51.  
  52.     public void Move (float rollInput, float pitchInput, float yawInput, float throttleInput, bool airBrakes) {
  53.         this.RollInput = rollInput;
  54.         this.PitchInput = pitchInput;
  55.         this.YawInput = yawInput;
  56.         this.ThrottleInput = throttleInput;
  57.         this.AirBrakes = airBrakes;
  58.  
  59.         ClampInput();
  60.         CalculateRollAndPitchAngles();
  61.         AutoLevel();
  62.         CalculateForwardSpeed();
  63.         ControlThrottle();
  64.         CalculateDrag();
  65.         CalculateLinearForces();
  66.         CalculateTorque();
  67.         if (Throttle < 0.1f) {
  68.             Vector3 currentVelocity = rigidBody.velocity;
  69.             Vector3 newVelocity = currentVelocity * Time.deltaTime;
  70.             rigidBody.velocity = currentVelocity - newVelocity;
  71.         }
  72.     }
  73.  
  74.     void ClampInput() {
  75.         RollInput = Mathf.Clamp (RollInput, -1, 1);
  76.         PitchInput = Mathf.Clamp (PitchInput, -1, 1);
  77.         YawInput = Mathf.Clamp (YawInput, -1, 1);
  78.         ThrottleInput = Mathf.Clamp (ThrottleInput, -1, 1);
  79.     }
  80.  
  81.     void CalculateRollAndPitchAngles() {
  82.         Vector3 flatForward = transform.forward;
  83.         flatForward.y = 0;
  84.         if (flatForward.sqrMagnitude > 0) {
  85.             flatForward.Normalize();
  86.             Vector3 localFlatForward = transform.InverseTransformDirection(flatForward);
  87.             PitchAngle = Mathf.Atan2(localFlatForward.y,localFlatForward.z);
  88.  
  89.             Vector3 flatRight = Vector3.Cross(Vector3.up, flatForward);
  90.  
  91.             Vector3 localFlatRight = transform.InverseTransformDirection(flatRight);
  92.  
  93.             RollAngle = Mathf.Atan2(localFlatRight.y, localFlatRight.x);
  94.         }
  95.     }
  96.     void AutoLevel() {
  97.         BankedTurnAmount = Mathf.Sin (RollAngle);
  98.         if (RollInput == 0f) {
  99.             RollInput = -RollAngle*AutoRollLevel;
  100.         }
  101.         if (PitchInput == 0f) {
  102.             PitchInput = -PitchAngle*AutoPitchLevel;
  103.             PitchInput -= Mathf.Abs (BankedTurnAmount*BankedTurnAmount*AutoTurnPitch);
  104.         }
  105.     }
  106.  
  107.     void CalculateForwardSpeed() {
  108.         Vector3 localVelocity = transform.InverseTransformDirection (rigidBody.velocity) ;
  109.         ForwardSpeed = Mathf.Max (0, localVelocity.z);
  110.     }
  111.  
  112.     void ControlThrottle() {
  113.         if (Immobilized) {
  114.             ThrottleInput = -0.5f;
  115.         }
  116.         Throttle = Mathf.Clamp01 (Throttle + ThrottleInput * Time.deltaTime * ThrottleChangeSpeed);
  117.         EnginePower = Throttle * MaxEnginePower;
  118.     }
  119.  
  120.     void CalculateDrag() {
  121.         float extraDrag = rigidBody.velocity.magnitude * DragIncreaseFactor;
  122.         rigidBody.drag = (AirBrakes ? (OriginalDrag + extraDrag) * AirBrakesEffect : OriginalDrag + extraDrag);
  123.         rigidBody.angularDrag = OriginalAngularDrag * ForwardSpeed / 100000 + OriginalAngularDrag; // Change the value according to the ship.
  124.     }
  125.  
  126.     void CalculateLinearForces() {
  127.         Vector3 forces = Vector3.zero;
  128.         forces += EnginePower * transform.forward;
  129.         rigidBody.AddForce (forces);
  130.     }
  131.  
  132.     void CalculateTorque() {
  133.         Vector3 torque = Vector3.zero;
  134.         torque += PitchInput * PitchEffect * transform.right;
  135.         torque += YawInput * YawEffect * transform.up;
  136.         torque += -RollInput * RollEffect * transform.forward;
  137.         torque += BankedTurnAmount * BankedTurnEffect * transform.up;
  138.  
  139.         rigidBody.AddTorque(torque * AeroFactor);
  140.     }
  141.  
  142.     public void Immobilize() {
  143.         Immobilized = true;
  144.     }
  145.  
  146.     public void Reset() {
  147.         Immobilized = false;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement