zero3growlithe

AircraftController

Jun 23rd, 2020
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.70 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class AircraftController : MonoBehaviour
  7. {
  8.     #region MEMBERS
  9.  
  10.     [Header("[ References ]")]
  11.     [SerializeField]
  12.     private Rigidbody targetRigidbody;
  13.     [SerializeField]
  14.     private Transform targetBody; // child transform to animate for steering and pitching
  15.  
  16.     [Header("[ Maneuverability ]")]
  17.     [SerializeField]
  18.     private float accelerationSpeed = 1;
  19.     [SerializeField]
  20.     private float decelerationSpeed = 1;
  21.  
  22.     [Space(5)]
  23.     [SerializeField]
  24.     private AnimationCurve engineForceCurve = AnimationCurve.Linear(0, 0, 1, 1);
  25.     [SerializeField]
  26.     private float maxSpeed = 300;
  27.  
  28.     [Space(5)]
  29.     [SerializeField]
  30.     private float verticalForce = 1; // global y-axis force for climb/descend
  31.     [SerializeField]
  32.     private float turningForce = 1; // torque force
  33.  
  34.     [Header("[ Controls ]")]
  35.     [SerializeField]
  36.     private float horizontalAxisSpeed = 1; // horizontal input lerp speed (how fast it reaches target x-axis analog input value)
  37.     [SerializeField]
  38.     private float verticalAxisSpeed = 1; // vertical input lerp speed (how fast it reaches target y-axis analog input value)
  39.  
  40.     [Header("[ Animation ]")]
  41.     [SerializeField]
  42.     private Vector2 shipBankSpeedsMultiplier = Vector2.one; // animation bank (roll) speed multiplier
  43.     [SerializeField]
  44.     private Vector2 shipBankAngles = Vector2.one; // max roll angle (positive)
  45.  
  46.     #endregion
  47.  
  48.     #region PROPERTIES
  49.  
  50.     // REFERENCES
  51.     public Rigidbody TargetRigidbody {
  52.         get {return targetRigidbody;}
  53.     }
  54.     public Transform TargetBody {
  55.         get {return targetBody;}
  56.     }
  57.  
  58.     // MANEUVERABILITY
  59.     private float AccelerationSpeed {
  60.         get {return accelerationSpeed;}
  61.     }
  62.     private float DecelerationSpeed {
  63.         get {return decelerationSpeed;}
  64.     }
  65.  
  66.     private AnimationCurve EngineForceCurve {
  67.         get {return engineForceCurve;}
  68.     }
  69.     private float MaxSpeed {
  70.         get {return maxSpeed;}
  71.     }
  72.  
  73.     private float VerticalForce {
  74.         get {return verticalForce;}
  75.     }
  76.     private float TurningForce {
  77.         get {return turningForce;}
  78.     }
  79.  
  80.     // CONTROLS
  81.     private float HorizontalAxisSpeed {
  82.         get {return horizontalAxisSpeed;}
  83.     }
  84.     private float VerticalAxisSpeed {
  85.         get {return verticalAxisSpeed;}
  86.     }
  87.  
  88.     // ANIMATIONS
  89.     private Vector2 ShipBankSpeedsMultiplier {
  90.         get {return shipBankSpeedsMultiplier;}
  91.     }
  92.     private Vector2 ShipBankAngles {
  93.         get {return shipBankAngles;}
  94.     }
  95.  
  96.     // VARIABLES
  97.     private float CurrentThrustInput {get; set;}
  98.     private Vector2 CurrentSteerInput {get; set;}
  99.  
  100.     #endregion
  101.  
  102.     #region FUNCTIONS
  103.  
  104.     protected void Awake ()
  105.     {
  106.  
  107.     }
  108.  
  109.     protected void Start ()
  110.     {
  111.        
  112.     }
  113.  
  114.     protected void Update ()
  115.     {
  116.         HandleInput();
  117.         HandleShipAnimation();
  118.     }
  119.  
  120.     protected void FixedUpdate ()
  121.     {
  122.         HandleShipForces();
  123.     }
  124.  
  125.     protected void OnDestroy ()
  126.     {
  127.        
  128.     }
  129.  
  130.     private void HandleInput ()
  131.     {
  132.         // thrusting
  133.         float targetThrust = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.JoystickButton0) ? 1f : 0f;
  134.         float thrustDelta = targetThrust > CurrentThrustInput ? AccelerationSpeed : DecelerationSpeed;
  135.  
  136.         CurrentThrustInput = Mathf.MoveTowards(CurrentThrustInput, targetThrust, Time.deltaTime * thrustDelta);
  137.    
  138.         // steering
  139.         Vector2 steerInput = CurrentSteerInput;
  140.  
  141.         steerInput.x = Mathf.Lerp(steerInput.x, Input.GetAxisRaw("Horizontal"), Time.deltaTime * HorizontalAxisSpeed);
  142.         steerInput.y = Mathf.Lerp(steerInput.y, Input.GetAxisRaw("Vertical"), Time.deltaTime * VerticalAxisSpeed);
  143.  
  144.         CurrentSteerInput = steerInput;
  145.     }
  146.  
  147.     private void HandleShipAnimation ()
  148.     {
  149.         Vector3 localRotation = TargetBody.localEulerAngles;
  150.         Vector2 targetRotation = new Vector2(
  151.             CurrentSteerInput.x * ShipBankAngles.x,
  152.             CurrentSteerInput.y * ShipBankAngles.y);
  153.  
  154.         localRotation.z = Mathf.MoveTowardsAngle(localRotation.z, -targetRotation.x, ShipBankSpeedsMultiplier.x * Time.deltaTime);
  155.         localRotation.x = Mathf.MoveTowardsAngle(localRotation.x, targetRotation.y, ShipBankSpeedsMultiplier.y * Time.deltaTime);
  156.  
  157.         TargetBody.localEulerAngles = localRotation;
  158.     }
  159.  
  160.     private void HandleShipForces ()
  161.     {
  162.         float currentSpeed = Vector3.Dot(transform.forward, TargetRigidbody.velocity);
  163.        
  164.         float forwardForce = EngineForceCurve.Evaluate(CurrentThrustInput);
  165.         float targetSpeed = MaxSpeed * CurrentThrustInput;
  166.         float torque = TurningForce * CurrentSteerInput.x;
  167.         float liftForce = VerticalForce * -CurrentSteerInput.y;
  168.  
  169.         Vector3 finalForce = Vector3.up * liftForce * currentSpeed;
  170.  
  171.         if (currentSpeed < maxSpeed)
  172.         {
  173.             finalForce += transform.forward * forwardForce;
  174.         }
  175.        
  176.         TargetRigidbody.AddForce(finalForce, ForceMode.VelocityChange);
  177.         TargetRigidbody.MoveRotation(transform.localRotation * Quaternion.Euler(Vector3.up * torque));
  178.     }
  179.  
  180.     #endregion
  181.  
  182.     #region CLASS_ENUMS
  183.  
  184.     #endregion
  185. }
Add Comment
Please, Sign In to add comment