AeroplanePropellerAnimator.cs 3.5 KB

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