Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AircraftController : MonoBehaviour
- {
- #region MEMBERS
- [Header("[ References ]")]
- [SerializeField]
- private Rigidbody targetRigidbody;
- [SerializeField]
- private Transform targetBody; // child transform to animate for steering and pitching
- [Header("[ Maneuverability ]")]
- [SerializeField]
- private float accelerationSpeed = 1;
- [SerializeField]
- private float decelerationSpeed = 1;
- [Space(5)]
- [SerializeField]
- private AnimationCurve engineForceCurve = AnimationCurve.Linear(0, 0, 1, 1);
- [SerializeField]
- private float maxSpeed = 300;
- [Space(5)]
- [SerializeField]
- private float verticalForce = 1; // global y-axis force for climb/descend
- [SerializeField]
- private float turningForce = 1; // torque force
- [Header("[ Controls ]")]
- [SerializeField]
- private float horizontalAxisSpeed = 1; // horizontal input lerp speed (how fast it reaches target x-axis analog input value)
- [SerializeField]
- private float verticalAxisSpeed = 1; // vertical input lerp speed (how fast it reaches target y-axis analog input value)
- [Header("[ Animation ]")]
- [SerializeField]
- private Vector2 shipBankSpeedsMultiplier = Vector2.one; // animation bank (roll) speed multiplier
- [SerializeField]
- private Vector2 shipBankAngles = Vector2.one; // max roll angle (positive)
- #endregion
- #region PROPERTIES
- // REFERENCES
- public Rigidbody TargetRigidbody {
- get {return targetRigidbody;}
- }
- public Transform TargetBody {
- get {return targetBody;}
- }
- // MANEUVERABILITY
- private float AccelerationSpeed {
- get {return accelerationSpeed;}
- }
- private float DecelerationSpeed {
- get {return decelerationSpeed;}
- }
- private AnimationCurve EngineForceCurve {
- get {return engineForceCurve;}
- }
- private float MaxSpeed {
- get {return maxSpeed;}
- }
- private float VerticalForce {
- get {return verticalForce;}
- }
- private float TurningForce {
- get {return turningForce;}
- }
- // CONTROLS
- private float HorizontalAxisSpeed {
- get {return horizontalAxisSpeed;}
- }
- private float VerticalAxisSpeed {
- get {return verticalAxisSpeed;}
- }
- // ANIMATIONS
- private Vector2 ShipBankSpeedsMultiplier {
- get {return shipBankSpeedsMultiplier;}
- }
- private Vector2 ShipBankAngles {
- get {return shipBankAngles;}
- }
- // VARIABLES
- private float CurrentThrustInput {get; set;}
- private Vector2 CurrentSteerInput {get; set;}
- #endregion
- #region FUNCTIONS
- protected void Awake ()
- {
- }
- protected void Start ()
- {
- }
- protected void Update ()
- {
- HandleInput();
- HandleShipAnimation();
- }
- protected void FixedUpdate ()
- {
- HandleShipForces();
- }
- protected void OnDestroy ()
- {
- }
- private void HandleInput ()
- {
- // thrusting
- float targetThrust = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.JoystickButton0) ? 1f : 0f;
- float thrustDelta = targetThrust > CurrentThrustInput ? AccelerationSpeed : DecelerationSpeed;
- CurrentThrustInput = Mathf.MoveTowards(CurrentThrustInput, targetThrust, Time.deltaTime * thrustDelta);
- // steering
- Vector2 steerInput = CurrentSteerInput;
- steerInput.x = Mathf.Lerp(steerInput.x, Input.GetAxisRaw("Horizontal"), Time.deltaTime * HorizontalAxisSpeed);
- steerInput.y = Mathf.Lerp(steerInput.y, Input.GetAxisRaw("Vertical"), Time.deltaTime * VerticalAxisSpeed);
- CurrentSteerInput = steerInput;
- }
- private void HandleShipAnimation ()
- {
- Vector3 localRotation = TargetBody.localEulerAngles;
- Vector2 targetRotation = new Vector2(
- CurrentSteerInput.x * ShipBankAngles.x,
- CurrentSteerInput.y * ShipBankAngles.y);
- localRotation.z = Mathf.MoveTowardsAngle(localRotation.z, -targetRotation.x, ShipBankSpeedsMultiplier.x * Time.deltaTime);
- localRotation.x = Mathf.MoveTowardsAngle(localRotation.x, targetRotation.y, ShipBankSpeedsMultiplier.y * Time.deltaTime);
- TargetBody.localEulerAngles = localRotation;
- }
- private void HandleShipForces ()
- {
- float currentSpeed = Vector3.Dot(transform.forward, TargetRigidbody.velocity);
- float forwardForce = EngineForceCurve.Evaluate(CurrentThrustInput);
- float targetSpeed = MaxSpeed * CurrentThrustInput;
- float torque = TurningForce * CurrentSteerInput.x;
- float liftForce = VerticalForce * -CurrentSteerInput.y;
- Vector3 finalForce = Vector3.up * liftForce * currentSpeed;
- if (currentSpeed < maxSpeed)
- {
- finalForce += transform.forward * forwardForce;
- }
- TargetRigidbody.AddForce(finalForce, ForceMode.VelocityChange);
- TargetRigidbody.MoveRotation(transform.localRotation * Quaternion.Euler(Vector3.up * torque));
- }
- #endregion
- #region CLASS_ENUMS
- #endregion
- }
Add Comment
Please, Sign In to add comment