AeroplaneControlSurfaceAnimator.cs 5.2 KB

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