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