AeroplaneControlSurfaceAnimator.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using UnityEngine;
  3. #pragma warning disable 649
  4. namespace UnityStandardAssets.Vehicles.Aeroplane
  5. {
  6. public class AeroplaneControlSurfaceAnimator : MonoBehaviour
  7. {
  8. [SerializeField] private float m_Smoothing = 5f; // The smoothing applied to the movement of control surfaces.
  9. [SerializeField] private ControlSurface[] m_ControlSurfaces; // Collection of control surfaces.
  10. private AeroplaneController m_Plane; // Reference to the aeroplane controller.
  11. private void Start()
  12. {
  13. // Get the reference to the aeroplane controller.
  14. m_Plane = GetComponent<AeroplaneController>();
  15. // Store the original local rotation of each surface, so we can rotate relative to this
  16. foreach (var surface in m_ControlSurfaces)
  17. {
  18. surface.originalLocalRotation = surface.transform.localRotation;
  19. }
  20. }
  21. private void Update()
  22. {
  23. foreach (var surface in m_ControlSurfaces)
  24. {
  25. switch (surface.type)
  26. {
  27. case ControlSurface.Type.Aileron:
  28. {
  29. // Ailerons rotate around the x axis, according to the plane's roll input
  30. Quaternion rotation = Quaternion.Euler(surface.amount*m_Plane.RollInput, 0f, 0f);
  31. RotateSurface(surface, rotation);
  32. break;
  33. }
  34. case ControlSurface.Type.Elevator:
  35. {
  36. // Elevators rotate negatively around the x axis, according to the plane's pitch input
  37. Quaternion rotation = Quaternion.Euler(surface.amount*-m_Plane.PitchInput, 0f, 0f);
  38. RotateSurface(surface, rotation);
  39. break;
  40. }
  41. case ControlSurface.Type.Rudder:
  42. {
  43. // Rudders rotate around their y axis, according to the plane's yaw input
  44. Quaternion rotation = Quaternion.Euler(0f, surface.amount*m_Plane.YawInput, 0f);
  45. RotateSurface(surface, rotation);
  46. break;
  47. }
  48. case ControlSurface.Type.RuddervatorPositive:
  49. {
  50. // Ruddervators are a combination of rudder and elevator, and rotate
  51. // around their z axis by a combination of the yaw and pitch input
  52. float r = m_Plane.YawInput + m_Plane.PitchInput;
  53. Quaternion rotation = Quaternion.Euler(0f, 0f, surface.amount*r);
  54. RotateSurface(surface, rotation);
  55. break;
  56. }
  57. case ControlSurface.Type.RuddervatorNegative:
  58. {
  59. // ... and because ruddervators are "special", we need a negative version too. >_<
  60. float r = m_Plane.YawInput - m_Plane.PitchInput;
  61. Quaternion rotation = Quaternion.Euler(0f, 0f, surface.amount*r);
  62. RotateSurface(surface, rotation);
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. private void RotateSurface(ControlSurface surface, Quaternion rotation)
  69. {
  70. // Create a target which is the surface's original rotation, rotated by the input.
  71. Quaternion target = surface.originalLocalRotation*rotation;
  72. // Slerp the surface's rotation towards the target rotation.
  73. surface.transform.localRotation = Quaternion.Slerp(surface.transform.localRotation, target,
  74. m_Smoothing*Time.deltaTime);
  75. }
  76. // This class presents a nice custom structure in which to define each of the plane's contol surfaces to animate.
  77. // They show up in the inspector as an array.
  78. [Serializable]
  79. public class ControlSurface // Control surfaces represent the different flaps of the aeroplane.
  80. {
  81. public enum Type // Flaps differ in position and rotation and are represented by different types.
  82. {
  83. Aileron, // Horizontal flaps on the wings, rotate on the x axis.
  84. Elevator, // Horizontal flaps used to adjusting the pitch of a plane, rotate on the x axis.
  85. Rudder, // Vertical flaps on the tail, rotate on the y axis.
  86. RuddervatorNegative, // Combination of rudder and elevator.
  87. RuddervatorPositive, // Combination of rudder and elevator.
  88. }
  89. public Transform transform; // The transform of the control surface.
  90. public float amount; // The amount by which they can rotate.
  91. public Type type; // The type of control surface.
  92. [HideInInspector] public Quaternion originalLocalRotation; // The rotation of the surface at the start.
  93. }
  94. }
  95. }