AeroplanePropellerAnimator.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using UnityEngine;
  3. #pragma warning disable 649
  4. namespace UnityStandardAssets.Vehicles.Aeroplane
  5. {
  6. public class AeroplanePropellerAnimator : MonoBehaviour
  7. {
  8. [SerializeField] private Transform m_PropellorModel; // The model of the the aeroplane's propellor.
  9. [SerializeField] private Transform m_PropellorBlur; // The plane used for the blurred propellor textures.
  10. [SerializeField] private Texture2D[] m_PropellorBlurTextures; // An array of increasingly blurred propellor textures.
  11. [SerializeField] [Range(0f, 1f)] private float m_ThrottleBlurStart = 0.25f; // The point at which the blurred textures start.
  12. [SerializeField] [Range(0f, 1f)] private float m_ThrottleBlurEnd = 0.5f; // The point at which the blurred textures stop changing.
  13. [SerializeField] private float m_MaxRpm = 2000; // The maximum speed the propellor can turn at.
  14. private AeroplaneController m_Plane; // Reference to the aeroplane controller.
  15. private int m_PropellorBlurState = -1; // To store the state of the blurred textures.
  16. private const float k_RpmToDps = 60f; // For converting from revs per minute to degrees per second.
  17. private Renderer m_PropellorModelRenderer;
  18. private Renderer m_PropellorBlurRenderer;
  19. private void Awake()
  20. {
  21. // Set up the reference to the aeroplane controller.
  22. m_Plane = GetComponent<AeroplaneController>();
  23. m_PropellorModelRenderer = m_PropellorModel.GetComponent<Renderer>();
  24. m_PropellorBlurRenderer = m_PropellorBlur.GetComponent<Renderer>();
  25. // Set the propellor blur gameobject's parent to be the propellor.
  26. m_PropellorBlur.parent = m_PropellorModel;
  27. }
  28. private void Update()
  29. {
  30. // Rotate the propellor model at a rate proportional to the throttle.
  31. m_PropellorModel.Rotate(0, m_MaxRpm*m_Plane.Throttle*Time.deltaTime*k_RpmToDps, 0);
  32. // Create an integer for the new state of the blur textures.
  33. var newBlurState = 0;
  34. // choose between the blurred textures, if the throttle is high enough
  35. if (m_Plane.Throttle > m_ThrottleBlurStart)
  36. {
  37. var throttleBlurProportion = Mathf.InverseLerp(m_ThrottleBlurStart, m_ThrottleBlurEnd, m_Plane.Throttle);
  38. newBlurState = Mathf.FloorToInt(throttleBlurProportion*(m_PropellorBlurTextures.Length - 1));
  39. }
  40. // If the blur state has changed
  41. if (newBlurState != m_PropellorBlurState)
  42. {
  43. m_PropellorBlurState = newBlurState;
  44. if (m_PropellorBlurState == 0)
  45. {
  46. // switch to using the 'real' propellor model
  47. m_PropellorModelRenderer.enabled = true;
  48. m_PropellorBlurRenderer.enabled = false;
  49. }
  50. else
  51. {
  52. // Otherwise turn off the propellor model and turn on the blur.
  53. m_PropellorModelRenderer.enabled = false;
  54. m_PropellorBlurRenderer.enabled = true;
  55. // set the appropriate texture from the blur array
  56. m_PropellorBlurRenderer.material.mainTexture = m_PropellorBlurTextures[m_PropellorBlurState];
  57. }
  58. }
  59. }
  60. }
  61. }