LandingGear.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.Vehicles.Aeroplane
  4. {
  5. public class LandingGear : MonoBehaviour
  6. {
  7. private enum GearState
  8. {
  9. Raised = -1,
  10. Lowered = 1
  11. }
  12. // The landing gear can be raised and lowered at differing altitudes.
  13. // The gear is only lowered when descending, and only raised when climbing.
  14. // this script detects the raise/lower condition and sets a parameter on
  15. // the animator to actually play the animation to raise or lower the gear.
  16. public float raiseAtAltitude = 40;
  17. public float lowerAtAltitude = 40;
  18. private GearState m_State = GearState.Lowered;
  19. private Animator m_Animator;
  20. private Rigidbody m_Rigidbody;
  21. private AeroplaneController m_Plane;
  22. // Use this for initialization
  23. private void Start()
  24. {
  25. m_Plane = GetComponent<AeroplaneController>();
  26. m_Animator = GetComponent<Animator>();
  27. m_Rigidbody = GetComponent<Rigidbody>();
  28. }
  29. // Update is called once per frame
  30. private void Update()
  31. {
  32. if (m_State == GearState.Lowered && m_Plane.Altitude > raiseAtAltitude && m_Rigidbody.velocity.y > 0)
  33. {
  34. m_State = GearState.Raised;
  35. }
  36. if (m_State == GearState.Raised && m_Plane.Altitude < lowerAtAltitude && m_Rigidbody.velocity.y < 0)
  37. {
  38. m_State = GearState.Lowered;
  39. }
  40. // set the parameter on the animator controller to trigger the appropriate animation
  41. m_Animator.SetInteger("GearState", (int) m_State);
  42. }
  43. }
  44. }